XML and AS3 for beginners

May 15th, 2009

Hi guys, now we say something about how load and use the XML file as data base inside the Flash project using Actionscript 3.

First we need load the XML file to read these data inside the Flash. To do it create first a variable of URLLoader and other URLRequest. Different that when we load images, here need use the URLLoader() and not the Loader() class. Go to the first step:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
 
	public class loadXML
	{
		private var xmlLoader:URLLoader;
		private var xmlRequest:URLRequest;
		private var newXML:XML;
 
		public function loadXML():void
		{
			xmlLoader 	= new URLLoader();
			xmlRequest 	= new URLRequest("dataXML.xml");
			xmlLoader.load(xmlRequest);
			xmlLoader.addEventListener(Event.COMPLETE, dataLoaded);
		}
 
		public function dataLoaded(e:Event):void
		{
			newXML = new XML(e.target.data);
			trace(newXML);
		}
	} // End Class
} // End Package

The sample code will load the XML using URLLoader and using the URLRequest, inside the URLRequest you will put the XML file address. After that may create the Listener to say when XML file is loaded or loading or all that happened.

Inside the dataLoaded() function you will see that the newXML:XML() receiving the loaded data and creating your new XML file. Yes, now we can use the data to put inside the project, well lets go to the second step, first see the XML file:

1
2
3
4
5
6
< ?xml version="1.0" encoding="utf-8"?>
<gallery title="Gallery Name" author="Author Name">
	<picture title="title of the first picture">/images/files/001.jpg</picture>
	<picture title="title of the second picture">/images/files/002.jpg</picture>
	<picture title="title of the last picture">/images/files/003.jpg</picture>
</gallery>

This time we have all the xml data inside the newXML() variable, to use these data it’s so simple, first we need to think that the first tag of the XML file is our newXML variable, if we call the newXML.@title will retrieve the title of the gallery tag. The same can do it to retrieve the author parameter value.

Well, if you retrieve the data from the gallery tag the rest is so easy. Let’s now retrieve the picture value, see the sample inside the dataLoaded() function:

24
25
26
27
28
29
public function dataLoaded(e:Event):void
{
	newXML = new XML(e.target.data);
	trace(newXML.picture[0].text()); // result : /images/files/001.jpg
	trace(newXML.picture[0].@ title); // result : title of the first picture
}

Or you can take the picture tag number and make a loop with these data, to get the picture quantity you need only get the array lenght():

26
27
var newXMLQnt:uint = newXML.picture.length();
trace(newXMLQnt); // result : 3

Now you can create your own code using the sample codes, it’s so easy to do it, load and use the XML data inside the Flash using Actionscript 3. Try it and if have some difficulties comment here. Thanks for read and Cya.

Related Posts

3 Responses to “XML and AS3 for beginners”

  1. Peter Says:

    I’m rating it 8/10… worth watching on WikiBlast . n e t – here is for F.R.E.E

  2. Brandon Says:

    FAIL @ 1st comment…

  3. Harold Sindt Says:

    Let me start by saying excellent blog. Im unsure if it has been addressed, however when using Safari I can never get the whole site to load without refreshing several times. Could just be my laptop. Appreciate your work

Leave a Reply