Need help with XMLList in Flex

886 views Asked by At

I have XML with the following structure, for example

<root>
    <node flag="false"/>
    <node flag="true"/>
    <node flag="false"/>
    <node flag="false"/>
    <node flag="true">
        <node flag="false"/>
        <node flag="true"/>
        <node flag="false"/>
        <node flag="true"/>
    </node>
    <node flag="true"/>
    <node flag="false">
        <node flag="false"/>
        <node flag="true"/>
        <node flag="false"/>
        <node flag="true"/>
    </node>
    <node flag="false"/>
</root>

All children have name "node". What I need is to get an XMLList (or XML, no matter), with the same hierarchy, but containing only nodes with the flag "true".

The result I need for my example is:

<root>
    <node flag="true"/>
    <node flag="true">
        <node flag="true"/>
        <node flag="true"/>
    </node>
    <node flag="true"/>
</root>

Is there any nice way to do this using e4x (without iterating through loop)? I tried to do the following: xml.node.(@flag=="true"), but the result in this case is:

<root>
        <node flag="true"/>
        <node flag="true">
            <node flag="false"/> <!--need to kill this node-->
            <node flag="true"/>
            <node flag="false"/> <!--need to kill this node-->
            <node flag="true"/>
        </node>
        <node flag="true"/>
    </root>

Any ideas? Thank you!

2

There are 2 answers

5
Patrick On BEST ANSWER

Here a one liner in e4x as you ask :

xml..node.((@flag=="false") && (delete parent().children()[valueOf().childIndex()]))

it delete the node to the current XML so pay attention to have a copy of your current XML.

By the way you should know that e4x just do a loop under the hood, and that one liner will not be faster than a custom loop.

var xml:XML=<root>
    <node id="1" flag="false"/>
    <node id="2" flag="true"/>
    <node id="3" flag="false"/>
    <node id="4" flag="false"/>
    <node id="5" flag="true">
        <node id="5.1" flag="false"/>
        <node id="5.2" flag="true"/>
        <node id="5.3" flag="false"/>
        <node id="5.4" flag="true"/>
    </node>
    <node id="6" flag="true"/>
    <node id="7" flag="false">
        <node id="7.1" flag="false"/>
        <node id="7.2" flag="true"/>
        <node id="7.3" flag="false"/>
        <node id="7.4" flag="true"/>
    </node>
    <node id="8" flag="false"/>
</root>

trace("-- before --")
trace(xml.toXMLString())

xml..node.((@flag=="false") && (delete parent().children()[valueOf().childIndex()])) 

trace("\n-- after --")
trace(xml.toXMLString())
4
Mansuro On

It didn't kill those nodes, cause your condition xml.node.(@flag="true") works only on the direct children of root, you have to make another one for the children of node