Introduction to XML Print E-mail

Many server-side applications use XML to structure data, so you can use the XML classes in ActionScript to create sophisticated rich Internet applications, such as those that connect to web services. A web service is a means to connect applications—for example, an Adobe Flash Player 9 application and an application on a web server—through a common standard such as the Simple Object Access Protocol (SOAP).

The ECMAScript for XML specification in Adobe Flex defines a set of classes and functionality for working with XML data. These classes and functionality are known collectively as E4X. The two main classes you use in Flex are the XML and XMLList classes.

Note: There was an XML class in ActionScript 2.0. In ActionScript 3.0, it is renamed XMLDocument so that it does not conflict with the new XML class that is part of E4X. In ActionScript 3.0, the legacy classes — XMLDocument, XMLNode, XMLParser, and XMLTag — are included in the flash.xml package primarily for legacy support. The E4X classes are core classes; you need not import a package to use them. This Quick Start does not go into detail about the legacy ActionScript 2.0 XML classes. For details on these, see the flash.xml package in the Flex 3 Language Reference.

In the following example, you create an XML literal called myBooks by using ActionScript. You can create XML literals in ActionScript by writing XML in an ActionScript block and assigning it to a variable, because XML is a native data type in Flex, just like Number or Boolean.

The myBooks XML literal in the following example contains two book elements (also known as nodes). The first book element has four child elements, with the names title, author, amazonUrl, and pageCount.

To access the elements in an XML instance, you use dot notation (.), just like you do when accessing the property of an object. So, for example, to get a reference to the list of book nodes, you write myBooks.book. This returns an XMLList instance that contains the two book nodes in the myBooks XML. To access a specific node in a list of identical nodes, you use array notation. To get a reference to the first book node, for example, you write myBooks.book[0]. You should already be familiar with this use of the dot operator and array-style syntax if you have worked previously with Objects and Arrays in ActionScript. E4X, however, goes further than this and enables you to search the XML for a node with a specific attribute value.

In the following example, you get a reference to the first book by searching for the value of its ISBN attribute. Attributes in E4X are referenced by prefixing them with the at-sign, as in @ISBN. The statement myBooks.book.(@ISBN=="1590595181") translates to "find the book node where the ISBN attribute has a value of 1590595181." The other examples describe more advanced, query techniques.

Example



xmlns:mx="http://www.adobe.com/2006/mxml"
width="440" height="400"
initialize="initializeHandler();"
>


[CDATA[

[] public var a:XMLList;
[] public var b:XMLList;
[] public var c:XMLList;
[] public var d:XMLList;

// Model: XML structure describing
// some of the books in my collection.
[]
private var myBooks:XML =
<books>

<book ISBN="1590595181">
<title>Foundation ActionScript Animation: Making Things Movetitle>
<author>Keith Petersauthor>

<amazonUrl>http://tinyurl.com/npuxt
<pageCount>470pageCount>
book>

<book ISBN="1582346194">
<title>Send in the Idiots: Stories from the Other Side of Autismtitle>

<author>Kamran Nazeerauthor>
<amazonUrl>http://tinyurl.com/lo5ts
<pageCount>500pageCount>

book>
books>

private function initializeHandler():void

{
// An XML list that contains both book nodes.
a = myBooks.book;

// Keith Peters
b = myBooks.book[0].author;

// 470

c = myBooks.book.(@ISBN=="1590595181").pageCount;

// Delete the first book node.
delete myBooks.book[0];

// Send in the Idiots...
d = myBooks.book[0].title;
}
]]>





title="XML lookup results"
paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">

text="{'a: ' + a}" width="300"/>
text="{'b: ' + b}"/>
text="{'c: ' + c}"/>

text="{'d: ' + d}"/>