Sebastian Müller
Sebastian Müller

Reputation: 5589

change the characters of xml in actionscript

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

Upvotes: 1

Views: 147

Answers (1)

Hrundik
Hrundik

Reputation: 1838

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

Upvotes: 4

Related Questions