
Introduced with ActionScript 3.0 and Adobe Flash Player 9, ECMAScript for XML (E4X) offers new ways to retrieve data from XML in Adobe Flash applications. In this article I examine how you can use E4X to fetch data out of an XML object within an application in real time—processing only the information required to run the application. This strategy improves the performance of Flash applications by reducing unnecessary overhead and data loading.
The release of Flash Player 9 includes a new virtual machine called ActionScript Virtual Machine 2 (AVM2), which increases player performance. In addition, improvements to the ActionScript language in ActionScript 3.0 offer Flash developers new functionality when creating Flash applications.
Note: Read the AVM2 overview (PDF, 401k ) for more information about the instructions, associated data structures, and file format supported by ActionScript Virtual Machine 2.
One of those improvements was the adoption of ECMAScript for XML (E4X), a part of ECMAScript—the standardized scripting language popularly implemented as JavaScript. ActionScript is based on ECMAScript and Adobe is one of the contributors to the ECMAScript standard.
E4X is an extension to ECMAScript that adds XML as a native data type, adding operators like the dot (.) and the attribute identifier (@) to search and filter data. For ActionScript developers, this means XML is now a native data type in ActionScript 3.0. XML now comes with its own set of classes to manage and access data, making it much easier for Flash to access data within XML structures.
E4X offers faster and more logical access to XML data consumed by ActionScript 3.0 applications. You can use it at runtime to pull data directly from an XML object, which eliminates the need to write a specialized parser. Previously XML data had to be transformed from an XML packet into structured native data types, such as arrays of objects, in order for the application to use the data. This process is now much more intuitive and XML data is easier to consume using ActionScript 3.0.
If you are familiar with the basics of accessing data within an array of objects using either square brackets ([]) or dot notation (.), you'll immediately recognize some of the syntax used when working with E4X. In addition to these operators, the attribute identifier (@) is used when performing E4X operations.
In the example below, a string of characters is data typed as an XML object:
var myXML:XML = <employees> <employee id="1"> <fname>Frank</fname> <lname>Bacon</lname> <email>fbacon@company.com</email> </employee> <employee id="2"> <fname>Chris</fname> <lname>Wren</lname> <email>cwren@company.com</email> </employee> </employees>
In the code example below, the E4X operators I previously discussed are used to access data contained within the XML object:
trace(myXML.employee[0].fname); // Output: Frank trace(myXML.employee.(@id=="2").email); // Output: cwren@company.com trace(myXML.employee.(lname=="Bacon").email); // Output: fbacon@company.com
The first trace statement displays the first name of the
employee in the first node of XML.
The second trace statement filters data by the id attribute to find a case where the id value is equal to "2."
The third trace statement filters data by finding a node
whose value is equal to "Bacon."
Note: These filters are case-sensitive. Comparing the string "bacon" would not result in the same output.
You can use a for loop to iterate through all the
nodes of the XML file. In the code example below, the loop repeats and outputs
each employee's first name, last name, and e-mail address until the data for
both nodes has been accessed:
for each ( var property:XML in myXML.employee ) {
trace(property.fname + " " + property.lname);
trace(property.email);
trace("--------------------");
}
The rest of this article describes how to build an application that uses XML data and E4X to populate multilingual captions for a video display.
To follow along with this article, you'll need to install the following software and files:
This article assumes that you have an intermediate knowledge of ActionScript 3.0 and have some prior experience working with XML files.
Andrew Muller is a trainer, mentor, developer, blogger, author, and presenter who has been involved with the production and development of rich Internet applications in Australia since 2002. Andrew is an Adobe Certified Instructor for Flex, Flash, ColdFusion, and Connect; he is also an Adobe Community Expert. Andrew has written articles for Internet.au, Digital Media World, Australian Developer, and International Developer magazines. He is a regular blogger and feature writer for BuilderAu.com.au, and has spoken at Macromedia- and Adobe-related conferences both in the United States, New Zealand, and Australia. Andrew is senior designer/developer at webqem pty limited in Sydney.