Flash Fridays – Generic AS 3.0 XML Loader
Posted by metavurt under @ Flash Fridays | Permalink | | Leave A Comment
If you haven’t already realized it, XML has fast become the backbone of data exchange across multiple platforms, applications, and environments. XML is fun!
Because of that, having an understanding of XML, and being able to quickly use it (and abuse it?) inside Flash has become a requirement. By understanding, I mean, in the very least, being able to load XML data into your various applications and projects.
Here’s a simple XML loader, using AS 3.0, which is vastly different from AS 2.0. In AS 2.0, one had to “hack” it into submission, and some of the ways in which data was retrieved was a bit obtuse (IMHO). With AS 3.0, Flash has been revamped and now interacts with XML by implementing ECMAScript for XML (E4X), which is an official language extension for working with XML. Neato!
The files I’ve included in the zip at the end of this post are simpleXML.fla, XMLLoader.as and simple.xml. Let’s go through them:
simple.xml – all the data being pulled into Flash. Simple layout, basically a list of “superfriends”.
Bruce Wayne
Batmobile
Clark Kent
GMC Truck
Joe Kerr
Clownmobile
unknown
Modified Garbage Truck
Robert "Bob" Parr
POS Microcar
simpleXML.fla – the main Flash document. Imports XMLLoader and instantiates it. Has an accessible, dynamic text field on the stage named superText.
XMLLoader.as – does all the work. Loads XML from a defined XML source (passed from simpleXML’s call to XMLLoader). Once loaded, adds all data sequentially to a string variable, then uses that to populate the text field with the data parsed from the XML source.
package {
import flash.display.MovieClip; // enable access current timeline
import flash.text.TextField; // enable access text field
import flash.events.*; // enable reference to load event
import flash.net.*; // enable URL classes, methods
public class XMLLoader {
private var _url:URLRequest; // request for the data source
private var _xml:XMLList; // xml object
private var _loader:URLLoader; // actual loader
private var _list:String; // string var to encapsulate data
public var _text:TextField; // text field target
public function XMLLoader(url:String, mc:MovieClip) {
_text = mc.superText; // specify location of text field
_url = new URLRequest(url); // get request
_loader = new URLLoader(_url); // load requested data
_loader.addEventListener(Event.COMPLETE, XMLComplete);
}
private function XMLComplete(e:Event):void {
_xml = new XMLList(_loader.data); // _xml now contains the data as an XML List
XMLProcess();
}
private function XMLProcess() {
_list = "";
for each (var _friend:XML in _xml.*) {
if(_friend.title !== null) {
// for each child in the node,
// format a human-legible sentence with the data
_list += _friend.title + "'s name is really " + _friend.name +
" and drives a " + _friend.car + ".\n\n";
}
}
XMLShow();
}
private function XMLShow() {
_text.text = _list; // transfer formatted data to text field
}
}
}
Hope this shows first of all, how significantly easier it is to deal with XML in AS 3.0, and how flexible it is, enabling you to twist it, roll it, dice it anyway you want when you throw it at your project and/or application.
Happy Coding!

