Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » 02Hero XML » E4X – ActionScript 3.0
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

E4X – ActionScript 3.0

E4X is a library written in ActionScript 3.0 which accesses and processes XML documents. E4X, as we shall soon see, greatly simplifies the task of a programmer who wants to parse XML documents, when they know the format beforehand. The library API is very intuitive and flexible, which marks it as a breakthrough software library.

Let's first take a real world example which we can use to create an XML document, and then illustrate how to access, parse and process it inside ActionScript 3.0 code, using the E4X API. Consider the following scenario:

Channel-9 sports channel in Australia does extensive and informative coverage of all international Twenty20 Cricket matches, and to engage the viewers attention, displays interesting player trivia and statistics. One example statistic is regarding the type of action put in by a cricketer when he is batting, bowling and fielding. The actions are standing, walking & running. Each match is 200 minutes long. Here is a sample XML document that contains these categories of actions and related statistics in minutes:

<?xml version="1.0" encoding="UTF-8" ?>

<effort-stats>

<player-effort>

<effort-type>standing</effort-type>

<role id="bowler">40</role>

<role id="fielder">50</role>

<role id="batsman">80</role>

<player-effort>

</player-effort>

<effort-type>walking</effort-type>

<role id="bowler">60</role>

<role id="fielder">30</role>

<role id="batsman">50</role>

</player-effort>

<player-effort>

<effort-type>running</effort-type>

<role id="bowler">100</role>

<role id="fielder">120</role>

<role id="batsman">70</role>

</player-effort>

</effort-stats>

Suppose there is a need to compute average standing, walking and running times for all players of a team over the duration of a single cricket match. The ActionScript 3.0 code using the E4X API would look like:

//import flash and E4X libraries

//...

var ul:URLLoader = new URLLoader(new URLRequest("effort-stats.xml");

ul.addEventListener(Event.COMPLETE, onXML);

function onXML(e:Event):void{

var xml:XML = new XML(e.target.data);

var avg:Number;

for each(var eff:XML in xml.player-effort){

avg = 0;

for each(var role:XML in eff.role){

avg += parseFloat(role);

}

trace(eff.effort-type, avg/(eff.role.length()));

}

}

You can verify that the above code prints out the average time spent by a player standing, walking and running.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now