by
kirupa | 17 July 2007
In the previous page,
you got a brief overview of this tutorial and what the major parts of an XML
document area. In this page, we'll set off on our journey by learning how to
load XML data into your application.
When loading an XML file, there are several steps you need to follow. You first
need to load the XML file. Digging deeper, you need some way
of knowing when the XML file has fully loaded because you
cannot manipulate partially loaded data. This seems
complicated, but the built-in classes help you out.
Let's look at the code for loading an XML file:
- var xmlLoader:URLLoader
= new
URLLoader();
- var xmlData:XML
= new
XML();
-
- xmlLoader.addEventListener(Event.COMPLETE,
LoadXML);
-
- xmlLoader.load(new
URLRequest("http://www.kirupa.com/net/files/sampleXML.xml"));
-
- function LoadXML(e:Event):void
{
- xmlData =
new XML(e.target.data);
- trace(xmlData);
- }
When you paste the above code into the Actions window, test your application
by going to Control | Test Movie or by pressing press
Ctrl + Enter. When you preview your animation, you will see our XML file's contents displayed in the
Output window:
[ your XML file is displayed in your Output window ]
Now that you know that our code works, let's take a better look at the code
and understand how it helps you load an XML file into memory:
- var xmlLoader:URLLoader
= new
URLLoader();
- var xmlData:XML
= new
XML();
In these two lines you declare two variables called
xmlLoader and xmlData. The xmlLoader
variable is of type URLLoader, and the URLLoader
class helps you to load data from an external source such as a URL.
The xmlData variable is of type XML, and the
XML class provides you with a lot of functionality for accessing and
manipulating XML data. We'll be using XML objects throughout this tutorial, so
I'll describe XML objects more in the upcoming sections.
- xmlLoader.addEventListener(Event.COMPLETE,
LoadXML);
In this line of code, we register an event listener to our xmlLoader object.
An event listener basically, as its name implies, listens for a
certain event, and when that event occurs, calls a listener function.
In our line of code, we listen for the completed event (Event.COMPLETE),
and when we hear that event, we call the LoadXML listener function. Because we
wait for the COMPLETE event, we
ensure that we do not prematurely start fiddling with the data until all of
our data has been loaded.
- xmlLoader.load(new
URLRequest("http://www.kirupa.com/net/files/sampleXML.xml"));
We aren't done with our xmlLoader object yet. In the above line, we call our
xmlLoader's load method.
The load method only
takes a URLRequest object as its argument. The reason is, when you download data
from the internet, the data is downloaded piecewise as streams. The URLRequest
class ensures all of the data is loaded in its entirety, and that ensures our load method gets
all of the XML data at once.
Let's now look at the LoadXML method which I briefly mentioned when
discussing the addEventListener method earlier:
- function LoadXML(e:Event):void
{
- xmlData =
new XML(e.target.data);
- trace(xmlData);
- }
Your LoadXML listener function gets called when your xmlLoader's event
listener detects a COMPLETE event. The COMPLETE event only gets fired when all
of the external data via the load method gets fully loaded.
Because our LoadXML function is a listener, it is constructed a little
differently than your typical function. The LoadXML function takes one argument of type
Event called e,
and this e argument contains lots of data related to the event that fired it.
You
can access the data sent to our event listener by checking
e.target.data. Since our event listener is
fired by our URLLoader object, the data sent to it is the XML data you
loaded via the URLRequest earlier.
Finally, all of the data is stored in our XML object called xmlData. After
your LoadXML method has run its course, all of your XML data will then be stored
in memory.
Onwards to the
next page!
|