tags:

views:

370

answers:

2

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>
+1  A: 

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>
Robert Bak
Thanks! it was really simple after all.
eldamar
Just a note, as it's your second question and you haven't accepted any answers or voted on any. Please make sure how StackOverflow works by reading http://stackoverflow.com/faq, especially the "How do I ask questions here?" section. Cheers
Robert Bak
Sorry about that, didn't know about the accept answer thing. I tried to vote on answers but i didn't have enough points to do so? I'll have to look at the faq, thanks for the link.
eldamar
A: 

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++;
}
Patrick