Thursday, March 13, 2008

E4X (ECMAScript for XML)

E4X is a scripting language extension that adds native XML support to ECMAscript. It does this by providing access to the XML document in a form that feels natural for ECMAscript programmers. The goal is to provide a simpler API for accessing XML documents, than other common APIs, such as DOM or XSLT.

Lets take a look at a few examples of how you can read XML data using E4X.

You will be able to create variables of type XML by parsing a String. But XML literals will now be supported as well:


var employees:XML =
<employees>
<employee ssn="”123-123-1234″">
<name first="”John”" last="”Doe”/"></name>
<address>
<street>11 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>98765</zip>
</address>
</employee>
<employee ssn="”789-789-7890″">
<name first="”Mary”" last="”Roe”/"></name>
<address>
<street>99 Broad St.</street>
<city>Newton</city>
<state>MA</state>
<zip>01234</zip>
</address>
</employee>
</employees>
;

Instead of using DOM-style APIs like firstChild, nextSibling, etc., with E4X you just “dot down” to grab the node you want. Multiple nodes are indexable with [n], similar to the elements of an Array:


trace(employees.employee[0].address.zip);

98765

To grab an attribute, you just use the .@ operator:

If you don’t pick out a particular node, you get all of them, as an indexable list:



trace(employees.employee.name);


<name first="”John”" last="”Doe”/">
<name first="”Mary”" last="”Roe”/">

(And note that nodes even toString() themselves into formatted XML!)

A handy double-dot operator lets you omit the “path” down into the XML _expression_, so you could shorten the previous three examples to



trace(employees..zip[0]);
trace(employees..name);

You can use a * wildcard to get a list of multiple nodes or attributes with various names, and the resulting list is indexable:


trace(employees.employee[0].address.*);


<street>11 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>98765</zip>

trace([EMAIL PROTECTED]);

Doe

You don’t have to hard-code the identifiers for the nodes or attributes… they can themselves be variables:



var whichNode:String = “zip”;
trace(employees.employee[0].address[whichNode]);

98765

var whichAttribute:String = “ssn”;
trace([EMAIL PROTECTED]);

789-789-7890

A new for-each loop lets you loop over multiple nodes or attributes:



for each (var ssn:XML in [EMAIL PROTECTED])
{
trace(ssn);
}

123-123-1234
789-789-7890

Most powerful of all, E4X supports “predicate filtering” using the syntax .(condition), which lets you pick out nodes or attributes that meet a condition you specify using a Boolean _expression_. For example, you can pick out the employee with a particular social security number like this, and get her state:



var ssnToFind:String = “789-789-7890″;
trace(employees.employee.(@ssn == ssnToFind)..state);

MA

Instead of using a simple conditional operator like ==, you can also write a complicated predicate filtering function to pick out the data you need.

E4X has complete support for XML namespace.

Compared with the current XML support in the Adobe Flash, E4X allows you to write less code and execute it faster because more processing can be done at the native speed of C++.

No comments: