Archive for the ‘XML’ Category
Parse XML Data in ActionScript 3 Using E4X
E4X is a programming language extension that adds native XML support to ActionScript. This means that once you have some XML data loaded into Flash, you can reference and traverse that data by using E4X. For our example let’s assume that the XML below is what you have loaded into Flash. I’m assuming you already have a XML object assigned to a variable, but in this example I’m creating animalXML XML object. (Read: Load XML Using AS3)
var animalXML:XML = <animals>
<animal species="feline" type="domestic">
<name>Pookie</name>
<age>5</age>
</animal>
<animal species="canine" type="domestic">
<name>Spot</name>
<age>8</age>
</animal>
</animals>;
E4X Syntax Examples
animalXML.animal.name
- Returns all name nodes that are a child of the animal node
<name>Pookie</name>
<name>Spot</name>
animalXML.animal.@species
- A shortcut to select a node attribute instead of a node.
- NOTE: This shortcut only works if the attribute exists in all nodes in the list.
- For a more reliable method of attribute selection, use attribute(“species”) instead of @species.
animalXML.animal.(age >= 6)
- Parenthetical can filter nodes based on a given criteria.
- In this case, it will return all animal nodes that have an age of 6 or greater.
animalXML..age
- Using two dots (..) instead of one (.) will return all age nodes regardless of their depth in the XML node tree.
- This gives you the ability to search for descendants of a given node rather than children of it.
