2

While I'm having no problems parsing incoming XML, I can't seem to construct valid outgoing xml. This is my code:

   myXML =
   <INFO>
   <imgname>testimage.jpg</imgname>
   <totalCols>{totalCols}</totalCols>
   </INFO>;

//The XML up to this point traces the desired output, it's when I try to append with the for loop that problems arise:

for (var i:Number = 0; i<totalCols; i++)
   {
    var tags:XML = 
    <tags>
    <tagx> {tagDisplay[i].x} </tagx>
    <tagy> {tagDisplay[i].y} </tagy>
    <tagtext> {tagDisplay[i].tagTxt.text} </tagtext>
    </tags>;

    myXML.appendChild(tags);
   }

The desired output I want is:

    <INFO>
    <imgname>testimage.jpg</imgname>
    <totalCols>7</totalCols>
//for loop kicks in here:
    <tags>
    <tagx>100</tagx>
    <tagy>100</tagy>
    <tagtext>tag1</tagtext>
    </tags>
    <tags>
    <tagx>120</tagx>
    <tagy>120</tagy>
    <tagtext>tag2</tagtext>
    </tags>
...etc for the total number in the for loop.
    </INFO>

Really simple I know, but my code just doesn't seem to work with the for loop included! Any advice much appreciated.

1
  • Bah, people are telling you to ditch this and use string manipulation. Ignore them. Your way is better. Commented Nov 15, 2010 at 10:19

2 Answers 2

1

I can't see any reason to use substitution here, simple assignments are nice and clear:

for (var i:Number = 0; i < totalCols; i++) {
    var tags:XML = <tags></tags>;
    tags.tagx    = tagDisplay[i].x;
    tags.tagy    = tagDisplay[i].y;
    tags.tagtext = tagDisplay[i].tagTxt.text;
    myXML.appendChild(tags);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I just added this code to an empty FLA:

var totalCols:Number = 4;
var tagDisplay:Array = [
    {x:0, y:0, tagTxt:{text:"stuff"}},
    {x:0, y:0, tagTxt:{text:"stuff"}},
    {x:0, y:0, tagTxt:{text:"stuff"}},
    {x:0, y:0, tagTxt:{text:"stuff"}}
];

var myXML:XML =
   <INFO>
   <imgname>testimage.jpg</imgname>
   <totalCols>{totalCols}</totalCols>
   </INFO>;

for (var i:Number = 0; i<totalCols; i++)
{
    var tags:XML = 
    <tags>
    <tagx> {tagDisplay[i].x} </tagx>
    <tagy> {tagDisplay[i].y} </tagy>
    <tagtext> {tagDisplay[i].tagTxt.text} </tagtext>
    </tags>;

    myXML.appendChild(tags);
}

trace(myXML);

The response I got was:

<INFO>
  <imgname>testimage.jpg</imgname>
  <totalCols>4</totalCols>
  <tags>
    <tagx>0</tagx>
    <tagy>0</tagy>
    <tagtext>stuff</tagtext>
  </tags>
  <tags>
    <tagx>0</tagx>
    <tagy>0</tagy>
    <tagtext>stuff</tagtext>
  </tags>
  <tags>
    <tagx>0</tagx>
    <tagy>0</tagy>
    <tagtext>stuff</tagtext>
  </tags>
  <tags>
    <tagx>0</tagx>
    <tagy>0</tagy>
    <tagtext>stuff</tagtext>
  </tags>
</INFO>

I think that is exactly what you want, isn't it? I haven't changed your code other than some sample input.

1 Comment

Well that's interesting... it probably means my variables aren't being initialised properly. I'll have a look cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.