Home arrow Flash arrow Page 2 - Using XML and ActionScript with Flex Applications

Using XML and ActionScript with Flex Applications

In this conclusion to a five-part series on using ActionScript to build sophisticated programs in Flex, we will add XML to the equation. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 3 stars3 stars3 stars3 stars3 stars / 20
May 29, 2008
  1. · Using XML and ActionScript with Flex Applications
  2. · Reading XML Data
  3. · Writing to and Editing XML Objects
  4. · Reflection
  5. · Getting the Class by Name

print this article

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:


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();


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++) {


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:



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:


You can chain together this simple E4X syntax as in the following example, which retrieves the first author node of the first book node:


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:


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++) {

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials