ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Intro to E4X
http://www.actionscript.org/resources/articles/222/1/Intro-to-E4X/Page1.html
Hasan Otuome
Hasan Otuome is Chief Architect for Marx Media (http://www.marxmedia.net) where he can usually be found developing Rich Internet Applications for the company's clients. He champions creative uses and combinations of Flash, PHP, AJAX and MySQL, among others, for their benefits in user experience improvement. 
By Hasan Otuome
Published on September 3, 2006
 
Tutorial Details:
Written by:
Hasan Otuome
Time: 10-15 minutes
Difficulty: Beginner Intermediate
Requirements: Flash 9/Flex 2/ActionScript 3
Topics Covered: XML
Assumed Knowledge: Dealing with XML in ActionScript 2

You can grab the tutorial files here

This tutorial gives a brief introduction to ActionScript 3's new weapon for XML, E4X.

Oh, the Joy!!!

Up until now working with XML data in Flash hasn't been very intuitive. That was mainly due to the semantics of the ActionScript Language. But, with the birth of AS3 we have new hope in E4X. For those that don't know E4X stands for ECMAScript for XML and this specification introduces some new functionality that makes working with XML a lot simpler. New to AS3 are the XML, XMLList,QSpace and Namespace E4X classes. E4X not only makes life simpler but provides greater code consistency and familiarity (we can use dot syntax more similiar to ActionScript).

We'll start off by dealing with XML literals or inline XML then we'll look at external XML since most of the time you'll be loading XML from an external source. Here's how easy things have gotten for us:

[as]//create a new XML object
var company:XML =
 <employees>
  <employee id="1"><name>John</name><dept>IT</dept></employee>
  <employee id="2"><name>Susan</name><dept>Marketing</dept></employee>
   </employees>;[/as]

Simple enough right? Next, let's create a function for some reusable code to help illustrate:

[as]//create a printer function since we'll be reusing this routine
function echo(){
 //print the data to the output panel
 for each(var e in company.employee){
  trace("Employee: "+e.name+" works in the "+e.dept+" Dept.");
 }
}

//print the results
echo();
[/as]

We're just using a for..each loop to iterate through our XML and give us the info we want. How about adding a new employee?

[as]//to add a new employee
company.employee += <employee id="3"><name>Carl</name><dept>Research</dept></employee>;

//print the results
echo();
[/as]

Now, say you wanted to search the XML for a specific employee and change their department. Here's one way that could work:

[as]//assign Carl to a department
company.employee.(@id==3).dept = "IT";

//print the results
echo();[/as]

That would move Carl to the IT department. The (@id=3) is called a filter predicate (a/k/a - the condition to match) and it's just like the WHERE clause in a SQL statement...SELECT employee FROM company WHERE id = 3. So, we're grabbing the employee element(node) from the company XML tree where the employee attribute, id (@id) is equal to 3.

You might be saying "that's cool but, what about external XML?". Well, that's no problem. It's only slightly different than it was in AS2. To do it, we'll need to use
(2) new AS3 classes: URLLoader and URLRequest. URLRequest replaces getURL() and you can think of URLLoader like MovieClipLoader which allows us to monitor loading and do stuff in response to it. Ok, so with that in mind:

[as]var company:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("company.xml");

loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(event:Event):void{
   var loader:URLLoader = URLLoader(event.target);
   company = new XML(loader.data);
   trace(company.toXMLString());
}[/as]

Now we're cooking. But, how can we traverse our XML tree when we don't know the root node?

[as]
//to get to the array of child nodes
var nodes =  company.children();
[/as]

That effectively replaces XML.firstChild.childNodes and is totally more intuitive and easier to understand the relevancy.

Of course, in no means did we cover every aspect of E4X but I hope this introduction has sparked some ideas that you can expound upon and use to enhance your next application. Until next time...

Hasan
hasan [at] marxmedia dot net