Skip to content


Parsing XML with ActionScript 3

I just want to cover some of the basics to reading XML with AS3. I have learned that if you are going to be doing anything dynamically with AS3 you will have to know how to write and parse XML. Let’s get started.

First we look at writing the XML, here I have made an XML called namesList.xml.

<namesList>
  	 <name id="John">John</name>
 	<name id="Mike">Mike</name>
 	<name id="Pete">Pete</name>
 	<name id="Paul">Paul</name>
 	<name id="Mark">Mark</name>
</namesList>

As you can see in this example this xml has five elements, namesList, name * 4. The namesList is surrounding the name elements, which means that the element namesList holds those five names. You could have several more namesList that will hope more names but for this example we are keeping it simple. There are a couple of different ways to write the element. You can write it with no content like.

<name>John</name>      or like this
<name id="John"/>

Either way will get us the same result. How you write it here determines how you write the code to read it. I prefer the second way because you can add a lot of content to just one line. For example:

<name id=”John” lastname=”Smith” age=”35” status=”single” />

Now this one element holds all that data without having to create a lot of elements.

All right now lets write some code. I am using Flex and I have created a new ActionScript project so this may look a little different than if you doing it in the Flash IDE. The first things you need to do create are URLLoader, URLRequest and add a listener to the loader then load the request. The entire code looks like this. Oh by the way in the constructor I always pass this off to another function right away usually init(); Also make sure you import flash.net.URLLoader and import flash.net.URLRequest;

This creates a new URLLoader: this is the loader that will handle the xml

var ldr:URLLoader = new URLLoader();

This creates a new URLRequest: this is what gets the url or path to xml
< pre lang="xml" line="n" >var req:URLRequest = new URLRequest();

This is the line that puts the url into the URLRequest: here I have a variable named xmlURL that holds the string to the xml path.

req.url = xmlURL;

Here you add a listener that listens to when the xml has completed loading and then calls a function to handle the xml. In this case I named the function onCompleteHandler.

ldr.addEventListener(Event.COMPLETE, onCompleteHandler);

Then finally the loader loads the request.

ldr.load(req);

Now that it has loaded the function onCompleteHandler will process the XML.
Make sure you import flash.events.Event, or you will get an error.
Here “e” is the event and it holds the entire xml so we need to get that out of there.

private function onCompleteHandler(e:Event):void
		{
			here we create a variable that holds the xml
			var xml:XML = XML(e.target.data);
			then we trace the xml
			trace('xml ' + xml);
			tracexml
 <namesList>
  					 <name id="John">John</name>
 	<name id="Mike">Mike</name>
 	<name id="Pete">Pete</name>
 	<name id="Paul">Paul</name>
 	<name id="Mark">Mark</name>
</namesList>”
 
 
//now we create the XMLList that holds all the names.
//You have to include the .@ to get down to 
//the id part of the element.  
			var list:XMLList = xml.name.@id;
			//now we trace the list 
			trace('list ' + list.toString());
			tracelist 
<name id="John">John</name>
<name id="Mike">Mike</name>
<name id="Pete">Pete</name>
<name id="Paul">Paul</name>
<name id="Mark">Mark</name>”
 
 
			//now this is the loop that gets all of the names out. 
			//x holds each name and then we trace x.  
			for each(var x:XML in list)
			{
				trace('x ' + x);
			}
 
			trace” John Mike Pete Paul Mark”
		}

You can download for files here for further reference. I hope this helps you in parsing xml.

Share and Enjoy:
  • Facebook
  • Twitter
  • del.icio.us
  • Google Bookmarks

Posted in ActioinScript 3.

Tagged with , , , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Pixelhead says

    Nice simple explanation thx



Some HTML is OK

or, reply to this post via trackback.