Introduction to XML in Flash
       by senocular

Loading XML Into Flash
When you decide to use XML to load data into Flash, the next step is figuring out how exactly that XML file makes it there and what commands are needed to make it happen. Well, don't worry. It's not hard at all. If you've ever loaded in a variable string with loadVariables then you've pretty much already loaded XML too. It's the exact same process.

Loading XML revolves around 2 functions. One of these functions is a pre-existing function that you simply call yourself. However, the other is a callback function that you have to define which will automatically be called by Flash depending on the occurrence of a certain event. The event we're dealing with here is the event of the XML being fully downloaded and introduced into Flash movie. This is the called the onLoad event.

Each of of these functions are used on an XML instance. XML instances are created using the XML object, or class, and provide a construct in Flash that lets you manage your XML. If you're working at all with XML in Flash, then you're pretty much guaranteed to be using an XML instance.

So first, before anything, when loading XML into Flash, you have to create that XML instance. The XML instance for this example, and most you will see from now on despite the fact that naming is arbitrary, will be called "my_xml." Note: using "_xml" at the end of your XML variable name in Flash MX or MX04 will give you code hints. In MX04, typing a new XML instance will provide hints without the suffix (i.e. var life:XML = new XML(); suffices).

var my_xml = new XML();

In creating an XML instance in this manner, you do have the option of passing in an XML string to the XML constructor (the constructor being the function that creates the XML, here new XML()). That string would consist of XML which will be immediately defined within the XML instance created. For example:

var my_xml = new XML("<some>stuff</some>");

Though, when about to load in external XML, there's really no point since whenever you load XML into an XML instance in Flash, all XML contained within that instance is replaced with that which is loaded. Passing text like that is optional, so for this example it will just be omitted.

Once an instance exists, you can define the onLoad callback function. The callback function, whenever you make it, always has to be called onLoad. Just like other event handlers, this is how Flash knows to call it when its needed, i.e. when the XML content has been loaded and parsed. Additionally, a success argument is passed into each onLoad when it fires. This will let you know if your XML has actually successfully loaded or not. For example, when you try to load XML from the URL "http://get a life," success will be false - "http://get a life" is obviously not a valid URL (hey, we all have lives here!). However, use a valid url (with a rock-solid internet connection) and success will be true signifying the completion of XML being loaded into Flash and ready for use.

Here, we'll make an onLoad function that simply traces the XML object when successfully loaded. Tracing an XML object directly will reveal the XML in text format.

var my_xml = new XML();
my_xml.onLoad = function(success){
if (success){
trace(this);
}
}

Since onLoad is defined in the XML instance, this inside the function references the instance directly.

Now that the onLoad has been defined, it's now time to request the XML to load in an external XML document. This is handled through the load method, the second of the 2 functions. The load method accepts one argument, the external XML document's URL (this can be relative or absolute).

var my_xml = new XML();
my_xml.onLoad = function(success){
if (success){
trace(this);
}
}
my_xml.load("my_document.xml");

Now, supposing my_document.xml contained the following:

<myxml>
I can load XML like the wind!
</myxml>

When the ActionScript above is run and the XML is loaded, you would receive a trace that would resemble the following.

[ output of loaded xml trace ]

Bear in mind that the XML does have to load into Flash. This is not an immediate process. It takes time, often many seconds or Flash frames before any of the loaded XML is accessible through the XML instance. This means that any attempt to access that information in the same script which the load method is used will end in failure. That is, of course, unless you do so within the onLoad function. Though the onLoad is defined in the same script as everything else, it doesn't actually get executed until the XML is fully loaded and parsed - some time after the rest of the script has already completed running, So, in other words, don't do this:

var my_xml = new XML();
my_xml.onLoad = function(success){
if (success){
trace(this);
}
}
my_xml.load("my_document.xml");
trace(my_xml); <- too early, not loaded yet

 

It's in the onLoad function where you pretty much do everything it is you need to do with your loaded XML content. You need it to populate a menu? Do it in the onLoad. Want to display your family tree? Do it in the onLoad (someone has to have a nice XML family tree floating around). The onLoad is the key to handling loaded XML since it is at that point you actually have access to it. Anywhere else and you just may not have any XML to reference.


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.