3. Parse XML using E4X syntax
Remember we're using this XML for all the test here:
Once the XML has been loaded and the mainXML instance has been succesfully created, we can refer to the mainXML instance as a native ActionScript object
For example we want to know the title inside the channel tag:
mainXML.channel.titlewhich will return the XML node <title>
If we want to get the text inside the tag we have to use the text() method of the XML class:
mainXML.channel.title.text()But instead of using the common dot notation to go straight to the node we want to read we can also use the double dot notation "..":
mainXML..title.text()Using the ".." notation means that the xml will be parsed looking for all the tags called "title".
which give us the same result as above for the fact we have in the XML file just one "title" tag. But what would happen if we wrote this:
mainXML..name.text()this is the result:
This is because in the XML we have more than one "name" node.
So if we would like to get the third name tag (the one with "Frank") we cen do the follwings (all valids):
mainXML..name.text()Give me an XMLList of nodes called "item", take the 2nd element of the array and read the xml text.
mainXML..item..name.text()Same as above but start getting an array of "item" nodes, get the 2nd element and then search for the "name" node
mainXML..item.author.name.text()Same as the previous one but once got the "item" array 2nd element navigate to the "name" node using a simple straight dot syntax
4. Using Expressions and attributes
mainXML..item.(@id=="003").author.name.text()this one introduces the E4X expressions and attributes. search the "item" tags and in the resulting XMLList search for the item with the attribute ( attributes are represented using the "@" symbol) id = 003. Then navigate to the name tag as before. Remember that within the expression () the scope is the "item" tag
mainXML..item.(author.@email=="email@example.com")..name.text()This is similar as before but instead of evaluating the attribute id of the item tag we're using a child node of the item tag for the expression.
5. Assign values
As we can read values from an XML we can also assign values. for example:
trace("before: " + mainXML..item.@id) mainXML..item.@id = "__test__" trace("after: " + mainXML..item.@id)The attribute "id" in the 3rd item node now has been changed. BUT remember that you can do this only if the resulting node is a single node, because if it's a list of nodes flash will throw you an exception.
Example of invalid code:
trace("before: " + mainXML..item.@id) mainXML..item.@id = "__test__" // ERROR!! trace("after: " + mainXML..item.@id)You can always know how many nodes are returned when typing an e4x syntax using length() merthod:
6. ConclusionsFinally we have in flash a very powerful XML class (all the various XMLtoObjects as2 classes are useless now, xpath probably too) which has a powerful syntax and which renders xml as flash native objects
We have a validating parser, which will help many users to better understand XML
This is only a quick guide to e4x syntax, I suggest to read the ECMA pdf to better understand every aspect of this new "language"