Cristian
Cristian

Reputation: 15

Working with XML in flex

I'am working with an hierarchical xml structure like this:

<employee name="AAA" group="1"..../>
    <employee name="BBB" group="1"...>
        <employee name="CCC" group="1".../>
     </employee>  
    <employee name="DDD" group="0"... />
    <employee name="EEE" group="0"... />
</employee>

First I need to count all employee nodes (including root node). It must be: 5. I have tried using xml..employee.length() but it returns 4 (does not include the root node "AAA") If I tried xml.employee.length() only returns 1

Then, I have to create a XMList with specific search. For example all the nodes with attribute group="1"

The same problem occurs, I use: hitList:XMLList = xml..employee.(@group == "1") and returns the right result but it does not take in count the root node (in this case, it must be included)

How can I do this operations including the root node?

Thanks in advance

Cristian

Upvotes: 0

Views: 744

Answers (1)

Anton
Anton

Reputation: 4684

The problem is that a XML variable is identical to the root XML tag. So if you wright

xml..employee.length()

it means you analyze all the nodes under the root node. The variable xml is your root node.

To get a correct result you need to add an empty dummy root node in your structure.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600" creationComplete="init()">
<fx:Declarations>
    <fx:XML id="xml" xmlns="">
        <root>
            <employee name="AAA" group="1">
                <employee name="BBB" group="1">
                    <employee name="CCC" group="1"/>
                </employee>  
                <employee name="DDD" group="0"/>
                <employee name="EEE" group="0"/>
            </employee>
        </root>
    </fx:XML>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        private function init():void
        {
            var len:int = xml..employee.length(); //gives 5
            var hitList:XMLList = xml..employee.(@group == "1"); //gives 3 nodes

            trace();
        }
    ]]>
</fx:Script>

</s:Application>

Upvotes: 1

Related Questions