1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.