13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with XML<br />

Docum<strong>en</strong>t A single XML structure. An XML docum<strong>en</strong>t can contain any number of elem<strong>en</strong>ts (or can consist only of a<br />

single empty elem<strong>en</strong>t); however, an XML docum<strong>en</strong>t must have a single top-level elem<strong>en</strong>t that contains all the other<br />

elem<strong>en</strong>ts in the docum<strong>en</strong>t.<br />

Node Another name for an XML elem<strong>en</strong>t.<br />

Attribute A named value associated with an elem<strong>en</strong>t that is writt<strong>en</strong> into the op<strong>en</strong>ing tag of the elem<strong>en</strong>t in<br />

attribut<strong>en</strong>ame="value" format, rather than being writt<strong>en</strong> as a separate child elem<strong>en</strong>t nested inside the elem<strong>en</strong>t.<br />

The E4X approach to XML processing<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The ECMAScript for XML specification defines a set of classes and functionality for working with XML data. These<br />

classes and functionality are known collectively as E4X. ActionScript 3.0 includes the following E4X classes: XML,<br />

XMLList, QName, and Namespace.<br />

The methods, properties, and operators of the E4X classes are designed with the following goals:<br />

Simplicity—Where possible, E4X makes it easier to write and understand code for working with XML data.<br />

Consist<strong>en</strong>cy—The methods and reasoning behind E4X are internally consist<strong>en</strong>t and consist<strong>en</strong>t with other parts of<br />

ActionScript.<br />

Familiarity—You manipulate XML data with well-known operators, such as the dot (.) operator.<br />

Note: There is a differ<strong>en</strong>t XML class in ActionScript 2.0. In ActionScript 3.0 that class has be<strong>en</strong> r<strong>en</strong>amed as<br />

XMLDocum<strong>en</strong>t, so that the name does not conflict with the ActionScript 3.0 XML class that is part of E4X. In<br />

ActionScript 3.0, the legacy classes—XMLDocum<strong>en</strong>t, XMLNode, XMLParser, and XMLTag—are included in the<br />

flash.xml package primarily for legacy support. The new E4X classes are core classes; you need not import a package to<br />

use them. For details on the legacy ActionScript 2.0 XML classes, see the flash.xml packagein the ActionScript 3.0<br />

Refer<strong>en</strong>ce for the Adobe Flash Platform.<br />

Here is an example of manipulating data with E4X:<br />

var myXML:XML =<br />

<br />

<br />

burger<br />

3.95<br />

<br />

<br />

fries<br />

1.45<br />

<br />

<br />

Oft<strong>en</strong>, your application will load XML data from an external source, such as a web service or a RSS feed. However, for<br />

clarity, the code examples provided here assign XML data as literals.<br />

As the following code shows, E4X includes some intuitive operators, such as the dot (.) and attribute id<strong>en</strong>tifier (@)<br />

operators, for accessing properties and attributes in the XML:<br />

trace(myXML.item[0].m<strong>en</strong>uName); // Output: burger<br />

trace(myXML.item.(@id==2).m<strong>en</strong>uName); // Output: fries<br />

trace(myXML.item.(m<strong>en</strong>uName=="burger").price); // Output: 3.95<br />

Last updated 6/6/2012<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!