as3 create xml basic question
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>1开发者_Python百科00</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.
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);
}
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.
精彩评论