XML in ActionScript 3
ActionScript's powerful XML support is based on E4X.
Let's see ActionScript's XML handling in action with some examples.
Basic Examples
The below examples use the following XML
var xmlAlbum:XML = <album name="Sky Blue Sky" artist="Wilco"> <tracks> <track name="Either Way" seconds="187"/> <track name="You Are My Face" seconds="278"/> <track name="Impossible Germany" seconds="358"/> <track name="Sky Blue Sky" seconds="203"/> <track name="Side With The Seeds" seconds="256"/> <track name="Shake It Off" seconds="342"/> <track name="Please Be Patient With Me" seconds="199"/> <track name="Hate It Here" seconds="273"/> <track name="Leave Me (Like You Found Me)" seconds="250"/> <track name="Walken" seconds="267"/> <track name="What Light" seconds="216"/> <track name="On And On And On" seconds="241"/> </tracks> </album>
Attribute Access
First let's do some investigating of the XML object. The variable (xmlAlbum) references the top-level element (<album>).
// XML element name var elementName:String = xmlAlbum.name(); // returns "album" // get the "name" attribute value var albumName:String = xmlAlbum.@name; // returns "Sky Blue Sky" // get the "artist" attribute value var albumArtist:String = xmlAlbum.@artist; // returns "Wilco" // set a "releaseDate" attribute xmlAlbum.@releaseDate = 2007; // the attribute is added
Child Access
Let's "walk the tree" a little...
// select all the tracks as an XMLList var xmlTracks:XMLList = xmlAlbum.tracks.track; // get the length of an XMLList var numTracks:Number = xmlTracks.length; // = xmlAlbum.tracks.track.length; // select the first track (similar to an array) var xmlFirstTrack:XML = xmlTracks[0]; // = xmlAlbum.tracks.track[0]; // select the fourth track var xmlFourthTrack:XML = xmlAlbum.tracks.track[3]; // remember, 0-based collections // get the index of an element (0-based) xmlFourthTrack.childIndex();
Looping over Children
An XMLList can be looped over like any other collection, using "for each".
// create a loop variable for holding the current track var xmlTrack:XML; // use for each to loop over XML items in an XMLList for each( xmlTrack in xmlAlbum.tracks.track ) { // do something with each track trace( xmlTrack.@name + " is track number " + (xmlTrack.childIndex()+1) ); }
Adding Elements
Let's add a child <description> element to the <album>.
// a string describing the album var description:String = "Sky Blue Sky is the sixth studio album by Chicago rock band Wilco, released on May 15, 2007 by Nonesuch Records."; // create an XML element var xmlDescription:XML = <description></description>; // set the text inside the element xmlDescription.appendChild( description ); // append the <description> element as a child of <album> xmlAlbum.appendChild( xmlDescription );
Deleting Elements
XML To/From a String
Convert an XML variable to a string using the toXMLString() function.
var albumData:String = xmlAlbum.toXMLString();
Note the releaseDate attribute and <comment> element we added.
<album name="Sky Blue Sky" artist="Wilco" releaseDate="2007"> <tracks> <track name="Either Way" seconds="187"/> <track name="You Are My Face" seconds="278"/> <track name="Impossible Germany" seconds="358"/> <track name="Sky Blue Sky" seconds="203"/> <track name="Side With The Seeds" seconds="256"/> <track name="Shake It Off" seconds="342"/> <track name="Please Be Patient With Me" seconds="199"/> <track name="Hate It Here" seconds="273"/> <track name="Leave Me (Like You Found Me)" seconds="250"/> <track name="Walken" seconds="267"/> <track name="What Light" seconds="216"/> <track name="On And On And On" seconds="241"/> </tracks> <description> Sky Blue Sky is the sixth studio album by Chicago rock band Wilco, released on May 15, 2007 by Nonesuch Records. </description> </album>
Convert a String variable to XML by using the top-level XML() function.
var string:String = "<data>...</data>"; var xml:XML = XML( string );
Advanced Examples
The example below demonstrates how to find elements in XML whose attributes meet certain criteria.
// find all tracks over 4 minutes (240 seconds) long var xmlLongSongs:XMLList = xmlAlbum.tracks.track.(@seconds > 240); // find the track called "Walken" // note that we reference the first element of the XMLList var xmlWalken:XML = xmlAlbum.tracks.track.(@name == "Walken")[0];
Below demonstrates how to loop over child elements.
// find the total length of the album var totalSeconds:Number = 0; var xmlTrack:XML; for each( xmlTrack in xmlAlbum.tracks.track ) { totalSeconds += Number( xmlTrack.@seconds ); } trace( "The album is " + totalSeconds + " total seconds" );
Below we'll loop over a list of attributes collected.
// find the total length of the album by looping over attributes var totalSeconds:Number = 0; var attrSeconds:XML; for each( attrSeconds in xmlAlbum.tracks.track.attribute("seconds") ) { totalSeconds += Number( attrSeconds ); } trace( "The album is " + totalSeconds + " total seconds" );

