Good lord, I just got a bit time to explore the new AS3 language, and my troubles already started.
No just joking,
Let’s get back to reality, it’s in deed quite different then with AS2 where you created an XML object and had to go through the all known firstchild, secondchild, bastard child, to finally be able to get your data, sure you could also install some classes from bit101 which gave you the ability to surf though an xml with XPath.
Though I think that the improvement Adobe did meeting the E4X regulations is much nicer.
First let us check our XML file, I’m using this XML file because I’m soon releasing a new version of my MP3 player completely made with AS3 so, this is how my XML looks like:
<playlist> <track> <title>TestTrack 1</title> <band>Band1</band> <file>test1.mp3</file> </track> <track> <title>TestTrack 2</title> <band>Band2</band> <file>test2.mp3</file> </track> <track> <title>TestTrack 3</title> <band>Band3</band> <file>test3.mp3</file> </track> <track> <title>TestTrack 4</title> <band>Band4</band> <file>test4.mp3</file> </track> </playlist>
If you compare with AS2, nothing big has changed besides of me getting older and more experienced, in other words I’ve stopped using attributes for everything
Next we are going to have a look at our actionscript which parses the XML.
Just open a new document and just for the matter of organization save it on the same location as your XML file, we should keep basics simple and not complicated.
On your newly created document, press F9 to bring up the actions panel, and as soon as you start typing Flash will show you the possible options you have for class you are loading in, I know that was already available on previous versions, but this time, it’s really helpful specially when you begin to learn a new language, by the way, I constantly try to keep the help panel open refering to the AS3 language reference.
With AS3 you using the new URLLoader class which somehow replaces the XML object from the early days, here we define a variable called xmlLoader which will be our XML container.
var xmlLoader:URLLoader = new URLLoader();
If you remember how you loaded XML with Flash 8 or lower, you for sure know the “noob” way of checking if it was loaded or not with the if.. statement, now we add Event Listeners to our containers and assign them an action to do when the event has been completed.
In our case we added an Event Listener to the xmlLoader container which tells us when the XML has been loaded and as soon that happens, the listener should call up the function named showXML.
xmlLoader.addEventListener(Event.COMPLETE, showXML);
this stayed ore or less the same like on AS2, below we’re loading the XML file into our xmlLoader container, keep in mind that URLRequests are ALWAYS string based
xmlLoader.load(new URLRequest("playlistAS3.xml"));
Basically our XML file has been loaded already, now we just want to display it very briefly to see if everything is running smoothly, for that we create a function called showXML.
function showXML(e:Event):void {
We ignore our white spaces, as before on AS2
XML.ignoreWhitespace = true;
We give our XML a variable called songs, I try to keep my variables and the XML as tight as possible so if my main child is called bugs, I would also call my variable bugs, got it?
var songs:XML = new XML(e.target.data);
If I would like to see how many items / nodes I have on my XML you just need to call the XML up and with the (.) syntax navigate through the XML till you reach your firstChild and check how many of those are around with the length() statement.
trace(songs.track.length());//Result is 4
I don’t think we need to explain this, anyhow, you create a variable called i and defined as a Number (numbers always start with 0)
var i:Number;
Do you remember for loops? No need to panic, they stayed the same ![]()
As a reminder: i equals 0, as long as i isn’t greater then the length of the xml, add one number to the variable i
for (i=0; i < songs.track.length(); i++) {
And lastly since you want to see that your code is really working let’s create a short trace block, displaying the contents of each node, declaring the title of the song as well the filename of the song.
To browser through the XML, you just need to point to your XML variable, remember it’s called songs, we add our little i to that variable so flash knows which instance it needs to be taken from, add the desired node name to it, in our case title and display with the text() statement.
trace(" Name of the song: "+ songs.track[i].title.text());
trace(" File Location of the song: "+ songs.track[i].file.text()); } }
Here the complete code for reviewing:
var xmlLoader:URLLoader = new URLLoader(); xmlLoader.addEventListener(Event.COMPLETE, showXML); xmlLoader.load(new URLRequest("playlistAS3.xml")); function showXML(e:Event):void { XML.ignoreWhitespace = true; var songs:XML = new XML(e.target.data); trace(songs.track.length()); var i:Number; for (i=0; i < songs.track.length(); i++) { trace(" Name of the song: "+ songs.track[i].title.text()); trace(" Name of the Band: "+ songs.track[i].band.text()); trace(" File Location of the song: "+ songs.track[i].file.text()); } }
That was it, I hope you understand now the basics of the XML loading process with AS3, sure there is more to learn about it, and I definately advise to do so as well, keep an eye on the built-in flash help (which is by the way constantly updated).
It only lefts me wishing you a lot of fun working with AS3 and whenever you have a question, just drop me a note on my blog.
Tiago




@Stephen – you can append the name of the text box at the beginning of each print so it will look like something like this content_txt.text = content_txt.text + “Name of Song:” + …. hope it helps.
thanks a lot for sharing keep it up
Hello,
I’m trieng to load an xml the same way as you do. But problems appear when i try to run it on a webserver and when its inside a web page.
Any tips for that?