Using XML and ActionScript with Flex Applications - Reading XML Data
(Page 2 of 5 )
Once you have an XML object, you can read from the object. There are two basic ways in which you can read the data: by traversing the document object model (DOM) or by accessing the data using E4X syntax. The two techniques are not exclusive of one another: you can use them in conjunction with one another.
In each case that outputs an XML node, the following examples use thetoXMLString()method to format the XML node as a string.
When viewing the XML data in light of the DOM, treat it simply as a hierarchical structure of data consisting of parent and child nodes. When looking at the DOM, focus primarily on the structure rather than the content. You can retrieve all the content from an XML object by treating it in this manner, but you access the data by structure by stepping into the XML one node at a time. TheXMLclass defines a host of methods for retrieving DOM structure information, including the following:
children()
Thechildren()method returns anXMLListobject with all the child nodes of anXMLobject. TheXMLListclass implements a very similar interface to that of XML, and all of the methods discussed in this section apply to bothXMLandXMLList. AnXMLListobject is essentially an array ofXMLorXMLListobjects. You can even retrieve elements from anXMLListobject using array access notation. For example, the following code retrieves the book nodes as anXMLList. It then displays the first element from that list:
var bookNodes:XMLList = xml.children();
trace(bookNodes[0].toXMLString());
length()
Thelength()method returns the number of elements. ForXMLobjects, this always returns1. ForXMLList objects, it may return more than1. The following example illustrates thechildren()andlength()methods used in conjunction. This example displays the titles of each of the books:
var bookNodes:XMLList = xml.children();
for(var i:uint = 0; i < bookNodes.length(); i++) {
trace(bookNodes[i].children()[0].toXMLString());
}
parent()
You can retrieve the parent of anXMLorXMLList object using theparent()method. For example, the following displays the first book node by accessing the title node first, and then it retrieves the parent of that node:
trace(xml.children()[0].children()[0].parent().toXMLString());
attributes()
Theattributes()method returns anXMLListobject with all the data from the attributes contained within anXMLobject. You can call thename()method for each attribute in theXMLListto 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 theXMLobject 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());
}
Next: Writing to and Editing XML Objects >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|