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.
E4X: A welcome addition to Flash Player
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.
E4X operators
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 =
Frank
Bacon
fbacon@company.com
Chris
Wren
cwren@company.com
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("--------------------");}
This article was previously posted by Andrew Muller on http://www.adobe.com/devnet/flash/articles/filtering_data_e4x.html