Theattributes() method returns anXMLList object with all the data from the attributes contained within anXML object. You can call thename() method for each attribute in theXMLList to retrieve the name of the attribute as a string. You can then use that value as a parameter, which you can pass to theattribute() method of theXML object to retrieve the value of the attribute. The following example illustrates how this works:
var author0:XML = xml.children()[0].children()[1].children()[0];
var attributes:XMLList = author0.attributes();
var attributeName:String;
for(var i:uint = 0; i < attributes.length(); i++) {
attributeName = attributes[i].name();
trace(attributeName + " " + author0.attribute(attributeName));
}
As you can see, traversing the XML DOM is effective but laborious. Often, it’s far more effective to use E4X syntax, particularly when you already know the structure. E4X syntax allows you to access child nodes by name as properties of parent nodes. For example, the following accesses the first book node:
trace(xml.book[0]);
You can chain together this simple E4X syntax as in the following example, which retrieves the first author node of the first book node:
trace(xml.book[0].authors.author[0].toXMLString());
E4X also allows you to easily access attributes using the@ symbol. The following uses this syntax to retrieve the value of the first attribute of the author node:
trace(xml.book[0].authors.author[0].@first);
You can also use E4X filters. Filters are enclosed in parentheses within which you specify conditions. The following example retrieves all the author nodes in which the last attribute isKazoun :
var authors:XMLList = xml.book.authors.author.(@last == "Kazoun");
for(var i:uint = 0; i < authors.length(); i++) {
trace(authors[i].parent().parent().toXMLString());
}