views:

72

answers:

1

Hi community I have the following problem,

given this xml

<test>
    <comp>
     <id>1</id>
     <content>bar</content>
    </comp>
    <comp>
     <id>2</id>
     <content>foo</content>
    </comp>
</test>

I want to change the value of the content of the comp element with id 2. Therefore I loop with a foreach loop

for each (var x : XML in testXML.children()) {
        if (x.id == 2) {
         for each (var element : XML in x.Children()) {
          if (element.localName() == "content") {
           element = "new content";
          }
         }
        }
       }

now when reaching the point element = "new content"; my programm tells me I cannot give element the value new content as this is a string and element is xml. How can I achieve this?

Thanks in advance

Sebastian

+3  A: 

Try using E4X instead of loops.

To access the element use:

xml.comp.(id == 2).content

To modify it's contents, use

xml.comp.(id == 2).content.* = "new content";

Much shorter :)

Hrundik
Ah great thanks for that answer, I like E4X but did not know the conditions. This is great.
Xelluloid