开发者

Flex: Replacing a node in a XML object

开发者 https://www.devze.com 2022-12-17 20:38 出处:网络
I have looked at some of the related posts on this subject but i can\'t figure out how to solve my problem. I guess it has something to do with the fact that its monday.

I have looked at some of the related posts on this subject but i can't figure out how to solve my problem. I guess it has something to do with the fact that its monday.

Well, here goes. i have a XML object containing:

<root>
   <page>
      <text>
         <style properties=""/>
         <label> Text one</label>
      </text>
      <text>
         <style properties=""/>
         <label> Text two</label>
      </text>
   </page>
   <page>
      <text>
         <style properties=""/>
         <label> Text three</label>
      </text>
      <text>
         <style properties=""/>
         <label> Text four</label>
      </text>
   </page>
</root>

And i want to replace only the label node with a new one. i put the new ones in an XMLList but now im stuck at how im supose to replace the actual nodes. This is how the XMLList looks like:

<page>
   <text>
      <style properties=""/>
      <label> Replace the first one</label>
   </text>
   <text>
      <style properties=""/>
      <label> Replace the second one</label>
   </text>
</page>
<page>
   <text>
开发者_如何转开发      <style properties=""/>
      <label> Replace the third one</label>
   </text>
   <text>
      <style properties=""/>
      <label> Replace the fourth one</label>
   </text>
</page>


A simple example:

// xml = your XML object    
xml.page[0].text[0].label = 'new text';
xml.page[0].text[1].label = 'new text 2';
trace (xml.toXMLString());

returns:

<root>
  <page>
    <text>
      <style properties=""/>
      <label>new text</label>
    </text>
    <text>
      <style properties=""/>
      <label>new text 2</label>
    </text>
  </page>
  <page>
    <text>
      <style properties=""/>
      <label>Text three</label>
    </text>
    <text>
      <style properties=""/>
      <label>Text four</label>
    </text>
  </page>
</root>


You can use e4x to get the XMMList and then the parent function in a loop or doing what you want with the list:

var r:XML=<root>
  <page>
    <text>
      <style properties=""/>
      <label>new text</label>
    </text>
    <text>
      <style properties=""/>
      <label>new text 2</label>
    </text>
  </page>
  <page>
    <text>
      <style properties=""/>
      <label>Text three</label>
    </text>
    <text>
      <style properties=""/>
      <label>Text four</label>
    </text>
  </page>
</root>;

var xl:XMLList=r.page.text.label;
// use the first element of the list
xl[0].parent().label="i do what i want";


var i:int=0;
// or loop over each elment if the list
for each (var xml:XML in xl){
    xml.parent().label=i + " : " + xml.toString();
    i++;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号