Chapter 18. XML and E4X

Since Flash Player 5, ActionScript has included tools for working with XML-structured data. In ActionScript 1.0 and ActionScript 2.0, XML data was created and manipulated with the variables and methods of the built-in XML class (e.g., firstChild, nextSibling, appendChild( ), etc.). The XML class was based on the W3C Document Object Model, or DOM, a standard for interacting with XML documents programmatically (see http://www.w3.org/DOM).

As of ActionScript 3.0, the toolset for creating and manipulating XML has been completely overhauled. ActionScript 3.0 implements ECMAScript for XML (“E4X”), an official ECMA-262 language extension for working with XML as a native datatype. E4X seeks to improve the usability and flexibility of working with XML in ECMA-262-based languages (including ActionScript and JavaScript).

Understanding XML Data as a Hierarchy

Before we can learn to manipulate XML data with E4X, we must first understand the general principle of XML as hierarchical data. Both the legacy XML class and E4X treat XML data as a hierarchical tree in which each element and text block is considered a tree node (i.e., a branch or a leaf). For example, consider the XML fragment in Example 18-1. (An XML fragment is a section of XML excerpted from an XML document.)

Example 18-1. An example XML fragment
<BOOK ISBN="0141182806">
  <TITLE>Ulysses</TITLE>
  <AUTHOR>Joyce, James</AUTHOR>
  <PUBLISHER>Penguin Books Ltd</PUBLISHER>
</BOOK>

The elements <BOOK>, <TITLE>, <AUTHOR>, and ...

Get Essential ActionScript 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.