Flash
  Home arrow Flash arrow Page 2 - Using XML and ActionScript with Flex Appli...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Using XML and ActionScript with Flex Applications
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2008-05-29

    Table of Contents:
  • Using XML and ActionScript with Flex Applications
  • Reading XML Data
  • Writing to and Editing XML Objects
  • Reflection
  • Getting the Class by Name

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More Flash Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Flex 2," published by...
     

    Buy this book now. 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.

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT