Wednesday, June 18, 2008

Loading XML and Parsing with AS3

This is what the XML doc looks like:
<title>Kevin's Evil Face</title>
<desc>This is Kevin layin down some tracks. Boho Digitalia - February 2008</desc>
<desc>This is Matt singin some live bgv's</desc>
<title>The 4923</title>
<desc>Once upon a time, Matt wasn't in the band yet and Kevin played bass. Kilby Court - A Really Long Time Ago</desc>

And this is how you load it into Flash and parse it:
var thumbs:Array = new Array();
var images:Array = new Array();
var titles:Array = new Array();
var descs:Array = new Array();

///// vvvvvv LOAD XML vvvvvv //////
var loader:URLLoader = new URLLoader();
var xml:XML = new XML();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("images.xml"));
function loadXML(event:Event):void{
xml = new XML(;
var total:Number = xml.pic.length();
for(var i:int = 0; i < total; i++){
thumbs[i] = xml.pic.thumb.text()[i];
images[i] = xml.pic.image.text()[i];
titles[i] = xml.pic.title.text()[i];
descs[i] = xml.pic.desc.text()[i];

function loadThumbs():void{
// do whatever you need here

This will make 4 arrays (thumbs, images, titles, descs) and populates them with everything from your XML file. I still haven't taken the time to figure out how to parse it into a 2D array, so if anyone wants to tell me how to do it, that would rock.


Anonymous said...

dood you just saved my life. Thank you so much.

JP said...

Yes thanks for this!

nate said...

Thanks guys. Glad it helped you out.

nate said...
This comment has been removed by the author.