Simple XML Loader Class

I’d like to present this here blog’s first piece of code: a simple helper class for loading XML into Actionscript 3.0. Its main purpose is to deal with drudgery of importing XML files into your Flash Projects, which happens in even my most basic projects these days. The class handles all the admin of error checking, and adds a cache killing feature while it’s at it. Groovy!

XMLLoaderExample.zip

Contains the Xml Loader class and the assets for this tutorial

If the thought of using custom classes in Flash leaves you dizzy, I’m planning a full tutorial on how to implement custom classes at some stage. For now though I present you with these simple steps on how to use the XMLLoader class in your project.

1. Extract the files to your project directory.

There are several ways to go about importing custom classes but my preferred method is to copy the class to the project folder itself. This insures that whenever you move the project around between different machines/setups the project will always have the classes it needs to work. It also means that updates to your Classes don’t break those old projects when you need to resuscitate them from the dead.

The folder structure relative to your fla file

The folder structure relative to your fla file

2. Import the class into Actionscript

Next you need to tell the Flash compiler where your class lives. We also create the variables that are going get our XML.

import com.monsterlove.net.XMLLoader;

var xmlLoader:XMLLoader;
var myXml:XML;

3. Initialize the xmlLoader

Next we create our xml loader. In order for it to work we need to pass it an URLRequest so Flash knows where to look for it.

xmlLoader = new XMLLoader(new URLRequest("data.xml"), false);
*Tip: if your swf file is being published to a different folder, urls will be relative to that folder. Navigating class directories and packages on the other hand is relative to your fla file.

If you would like to use the cache killing feature, so that your Flash Movie updates whenever the xml is modified, then you can pass true as your second parameter. This will only work if the file had been deployed to a server though! If you’re still testing locally Flash will probably throw errors. To clear your cache locally one needs to delete your temporary internet files.

4. Add an Event Listener

Loading XML is one of those things that happens asynchronously. That means once flash has made the request for the data it doesn’t know how long it will take to get a response, if indeed it gets any at all. As such we need an event listener so that Flash will only try to work with the xml once it’s all there. Under normal circumstances one would also have to add event listener in case something went wrong, but thankfully those joyless tasks are handled by the xml class and it will trace an Error should the file not load properly.

xmlLoader.addEventListener(Event.COMPLETE, onXMLComplete);

function onXMLComplete(e:Event):void {
}
5. Get the XML

In our listener, the first thing we need to do is remove our event listener. Event listeners tie up memory and you should always remove them once they have served their purpose. Now that we know the xml is loaded, we can safely use the xml property of xmlLoader to assign the xml to our own “myXML” object. You now have an xml object on which you can execute all your fiendish desires!

The finished script:

import com.monsterlove.net.XMLLoader;

var xmlLoader:XMLLoader;
var myXml:XML;

xmlLoader = new XMLLoader(new URLRequest("data.xml"), false);
xmlLoader.addEventListener(Event.COMPLETE, onXMLComplete);

function onXMLComplete(e:Event):void {
	xmlLoader.removeEventListener(Event.COMPLETE, onXMLComplete);
	myXml = xmlLoader.xml;
}
  • http://polprav.blogspot.com/ gagadget

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  • sam

    nice post, these helper classes are key to getting about the minutiae of any project.

    btw – download link is broken