Flash, Actionscript, Design, Games and Programming.
In today’s AS3 basics lesson, I’m focusing on XML. I’ll show you how to access an XML feed and parse out the data for use in your flash apps.
For this example, I’m going to use the brilliant last.fm api – and display the last 10 songs I listened to. For this, I’ll be using the user.getRecentTracks method.
So here’s the flash file -
[kml_flashembed fversion="9.0.0" movie="flash-content/lastXML/lastXML.swf" targetclass="flashmovie" publishmethod="static" width="400" height="165"]
[/kml_flashembed]
Here’s a section of the XML which is loaded by the flash file –
<lfm status="ok"> <recenttracks user="CatFurnace"> <track> <artist mbid="">Three Trapped Tigers</artist> <name>Untitled 4</name> .....etc
And the Actionscript code -
//First I make a textfield - to display the loaded data. var TF:TextField = new TextField(); TF.width = stage.stageWidth; TF.height = stage.stageHeight; TF.text = "Loading recent tracks..."; addChild(TF); //Set the location of the XML to load //Here I'm using my Last.fm recent tracks. var dataDir:String = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=catfurnace&api_key=b25b959554ed76058ac220b7b2e0a026"; //We tell the URLRequest what we want to load - var xmlString:URLRequest = new URLRequest(dataDir); //And create a URLLoader to send and load the data - var xmlLoader:URLLoader = new URLLoader(xmlString); //We then add a listener to the xmlLoader - which will perform the function "init", when the data has been sent, and the response loaded. xmlLoader.addEventListener("complete", init); //We also add a listener to an IO_ERROR event, which will run "xmlLoadFail", if there is an error. xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlLoadFail); //So if there is an error - write it in the text box. function xmlLoadFail(event:IOErrorEvent):void { TF.text = "Sorry - loading failed :-( "; } //This "init" function - which is called if the load request is successful - function init(event:Event):void { trace("loaded xml!"); //Clear the text box TF.text = ""; //Create a new XML document. var xDoc:XMLDocument = new XMLDocument(); //Ignore the white space xDoc.ignoreWhite = true; var XMLHolder:XML = XML(xmlLoader.data); //And parse the data xDoc.parseXML(XMLHolder.toXMLString()); //Count how many nodes are in the XML - var nodesToLoop:int = Number(xDoc.firstChild.firstChild.childNodes.length); //For every node in the XML, write the atrist and the track name in the text box. for (var j:int = 0; j<nodesToLoop; j++) { var artist:String = String(xDoc.firstChild.firstChild.childNodes[j].childNodes[0].firstChild.nodeValue); var track:String = String(xDoc.firstChild.firstChild.childNodes[j].childNodes[1].firstChild.nodeValue); //And add this data to the text box TF.appendText(artist+" - "+track+"\n") } } |
Hope that helps.
And whilst you’re here, why not add me on last.fm.
This is the blog of Lawrie Cape, an interactive developer from Leeds, England.
6 Responses to AS3 Basics 7 – Loading XML
dVyper
May 30th, 2009 at 2:52 pm
Good article. I notice though that you’re using the old way of reading the XML (node.firstChild.firstChild.childNodes etc) which is a bit longwinded. AS3 enables you to read XML in a MUCH easier way.
I think a good tutorial to learn about loading and using XML with AS3 is here:
http://www.gotoandlearn.com/play?id=64
Also you spelt ‘recent’ wrong on the 5th line.
dew
May 31st, 2009 at 1:08 am
Why you’re not using E4X?
for (node:XML in nodesToLoop) {
var artist:String = node.artist;
var track:String = node.track;
}
Lawrie
June 1st, 2009 at 6:22 pm
Hey guys, thanks for the comments. I’ve been meaning to get to grips with E4X for a while now, but haven’t got round to it yet. I’ll definitely get on it soon, and post an update for this. Cheers.
Sarah
June 2nd, 2009 at 7:15 pm
Can you tell I’ve been in your room?? I bet you are glad I wasn’t listening to anything embarrassing! That new Trail of Dead album is awesome!
P.S. I absolutely LOVE that tiny orange cat thing in the corner of the tab!
So you xx
Lawrie
June 6th, 2009 at 11:41 pm
I was wondering when I listened to so much Trail of Dead!
AS3 Basics 7.5 - Loading XML again | The Lawrie Cape Blog
June 9th, 2009 at 1:11 pm
[...] the last post, I showed one method of using XML in Flash. In the comments, a couple of people pointed out that [...]