How to Load XML in Action Script 3.0

In this tutorial, you will learn how to load XML in actionscript 3.0. XML is a language used to store and transfer data. They are structured forms that can be easily understood by both users and machines. There are no keywords associated with the tags. Hence user-defined tags are used. Follow this step by step guide to learn how to load xml in actionscript 3.



Step 1: Create a XML document
First let’s create a little XML document that has a list of tags. Save the file in the name “tag.xml”. The extension of the file is .xml.
Create a XML file

Step 2: Create new project
Open Adobe Flash and create a new project of type ActionScript 3.0.
Creating a New Project

Step 3: Open Actions Window
Now we are going to load a XML file into the actionscript Project. As we know, XML is not used to display data. Hence to know the successful loading of the XML, we have to trace the event. In order to do so, let’s open Actions Window with Layer1, Frame1 selected. Import the required files into flash.
Import Event class

Step 4: Add Event Listener
Next, add the code to load the XML into the project. A variable myXML of type XML is instantiated. We have an URLRequest to request the file. It searches for file in the path and the URLLoader loads the file. Add an EventListener as well, to trace the success or failure of the XML Loading. This event listener calls a function xmlloaded on complete loading of the XML file.
Code – XML Loading

Step 5: Trace function
The function xmlLoaded contains the functionality to give an output to the trace if the XML loading is complete. It gets the data from the URLLoader, myLoader. It traces back “Data Loaded” on completion of the XML Loading.
code -Trace function

Step 6: Test output of the movie
To test if the data has loaded, test the movie. Press the CTRL+Enter key. The output data in the flash window reads ‘data loaded’. Since XML is not a display language, the movie does not display any data. In this manner, you can load a xml file in actionscript.
Output –Trace event