Our concepting portfolio

Load XMl file in ActionScripting 3.0

First create the xml file and save as menu.xml

<site>
<links>
<link><![CDATA[home.html]]></link>
<title><![CDATA[HOME]]></title>
<image><![CDATA[images/1.png]]></image>
<des><![CDATA[car 1]]></des>
</links>
<links>
<link><![CDATA[about.html]]></link>
<title><![CDATA[ABOUT]]></title>
<image><![CDATA[images/2.png]]></image>
<des><![CDATA[car 2]]></des>
</links>
</links>
</site>


Note that the <![CDATA[car 2]]> code is just telling flash to treat it as expression or code.

 

In Flash enter the following lines:

var myXml:XML;

var myLoader:URLLoader = new URLLoader();

myLoader.load(new URLRequest("menu.xml"));
myLoader.addEventListener(Event.COMPLETE, runData);

 

function runData(e:Event):void{
myXml = new XML(myLoader.data);
myXml.ignoreWhitespace = true;
trace(myXml);
}


Let's go over the flash actionscript that you just entered.

The first two lines create an XML variable called myXML, and a URLLoader called myLoader.

var myXml:XML;

var myLoader:URLLoader = new URLLoader();

 

Next, we load the menu.xml into myLoader with the following code:

myLoader.load(new URLRequest("menu.xml"));

 

Next line, we add an event listener to run the runData function once the xml is completely loaded:

myLoader.addEventListener(Event.COMPLETE, runData);

 

The last bit is the runData function, we declare the function and pass in e as an event with no return value (as denoted in void):

function runData(e:Event):void{

our function here

}

 

We pass in the myLoader.data to myXml, set ignoreWhitespace to true and trace out myXml:

function runData(e:Event):void{
myXml = new XML(myLoader.data);
myXml.ignoreWhitespace = true;
trace(myXml);
}

 

Now that you have loaded the XML, let's access these values. We can access these listing with several operators. First is (.), the (@), and the square([]). The dot (.) enables you access elements of your XML, the (@) lets you access attributes of an element, and the square ([]) allows you to access a property by name or index.

 

trace(myXml.links[0].link); //Display: home.html

trace(myXml.links.(link.text() == "about.html").title); //Display: ABOUT

 

This is just a basic to Flash ActionScripting 3.0. I'm sure you will be able to find out more from the web.

 

DOWNLOAD file in this tutorial >