Don't understand this E4X behaviour
I'm trying to grab a property value of a particular node but getting stumped by its behaviour.
I have this XML in a data provider:
<items>
<item>
<code>ITEM1</code>
<groups>
<group>
<code>GROUP1</code>
<props>
<prop>
<code>PROP1</code>
</prop>
<prop>
<code>PROP2</code>
</prop>
</props>
</group>
</groups>
</item>
<!-- Lots more items, each item has a variable number of groups and props -->
</items>
Now, this code:
var itemData:XMLList = dataProvider..elements("item").(code == "ITEM1");
successfully returns the 1st <item>
.
Next, I do:
var groupData:XMLList = itemData..elements("group").(code == "GROUP1")
But that doesn't work. groupData.length() == 0
If I drop elements()
, then it works:
var groupData:XMLList = itemData..group.(code == "GROUP1")
But I need to use elements() beca开发者_JS百科use element names are potentially configurable.
So the question is, why does the first statement work, but using the same statement on the data returned by the first statement failed to work as expected?
Any help appreciated. Thanks in advance.
Because group is inner element of groups and not direct child of item and second statement is working because of recursive in nature
Try following code
var itemData:XMLList = xml..elements("item").(code == "ITEM1");
//Alert.show(itemData.toXMLString());
var groupsData:XMLList = itemData..elements("groups");
//Alert.show(groupsData.toXMLString());
var groupData:XMLList = groupsData..elements("group").(code == "GROUP1")
//Alert.show(groupData.toXMLString());
you can enable Altert to see expression result
EDITED Details for element function can be found at AS3 XML elements()
hopes that helps
精彩评论