Daniel
Daniel

Reputation: 35684

finding matching node within multilevel xml in as3

I've got a multilevel node structure that looks something like this

<node>
    <node>
        <node id="a1"></node>
        <node id="a2"></node>
    </node>

    <node>
        <node id="b1"></node>
        <node id="b2"></node>
    </node>
<node>

I want to do a search for the first node that matches an id.

I usually use this syntax:

xmldata.*.(@id == "a2")[0]

but it looks like it's not working for multiple nested levels. Is there a way of finding the node without looping through and archiving the content?

Upvotes: 1

Views: 989

Answers (2)

Michael Antipin
Michael Antipin

Reputation: 3532

Use descendants() or E4X operator ...

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#descendants%28%29

var test:XML = 
<node>
    <node id="b1"></node>
    <node id="b2"></node>
    <smth>
        <node id="b3">
            <smth>
                <node id="b4"></node>
            </smth>
        </node>
    </smth>
</node>;

var search:XMLList;

search = test.descendants("node").(attribute("id") == "b4");
trace(search.toXMLString());
// OR
search = test..node.(@id == "b4");
trace(search.toXMLString());

Note: use @id notation with caution. It will give you a reference error exception if any of nodes does not contain 'id' attribute.

Upvotes: 2

jerluc
jerluc

Reputation: 4316

While I don't fully understand your circumstances, have you tried using an XPath implementation?

Upvotes: 0

Related Questions