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.

4

2 回答 2

1

我刚刚将此代码添加到一个空的 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);

我得到的回应是:

<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>

我想这正是你想要的,不是吗?除了一些示例输入之外,我没有更改您的代码。

于 2010-11-15T10:11:04.333 回答
1

我看不出有任何理由在这里使用替换,简单的分配很好而且很清楚:

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);
}
于 2010-11-15T08:33:06.737 回答