Things have changed a little bit in AS3.0 when it comes to load XML but I am going to show you it is very easy to do once you done it a couple of times. In AS3.0 It is a bit different because as you know you need to write a bit more code to accomplish simple tasks. Let's have a look at the tutorial.
Tutorial
First let's create a little XML document that describe a list of fruits.
<fruits> </fruits> |
Save your XML list as fruits.xml and open a new AS3.0 document in Flash, on the first frame enter the code below:
import flash.net.*; var myFruitsData:URLRequest = new URLRequest("fruits.xml"); var myFruitsLoader:URLLoader = new URLLoader(); function completeListener(evt:Event){ |
1. The first two lines import the URLRequest, URLloader and Event classes. Note that if you are writing the code in a class you also need to import additional classes.
2. The first thing we are doing is to create a URLRequest object that will contain the path to the xml file. In AS2.0 this wasn't needed.
3. The next step is to create an URLloader object that will serve as a container for our XML document. myFruitsLoader will also serve as a loader that will inform us when it has loaded the entire document. The Event.COMPLETE is the AS3.0 version of our old onLoad AS2.0 event and it behaves the same way: When the data is loaded it will call the completeListener function where we will process the loaded data.
4. Once the data (loaded by default as TEXT) has completed loading the completeListener function is called, what we immediately do is to convert the loaded data into an XML object by creating an XML object from the evt.target.data data.
It is important to note here that it is the URLLoader object that contains the loaded data and that it can be access using myFruitsLoader.data or evt.target.data like in my example.
That's it basically, in the next tutorial I will show you how to access the loaded XML data.