Working with XML using Actionscript 3.0

August 10th, 2009

XML and Flash make a good team. I frequently use XML data to create Actionscript objects. XML provides an effective way of sending data to the flash player whether you are simply loading an external XML file or communicating with server-side scripts.

Actionscript 3.0 by using ECMAScript for XML (E4X) syntax makes work with XML a breeze. Below I will demonstrate the basics of working with XML using AS3, including loading, send, reading nodes value and reading attribute values.



Loading XML data with AS3:

function loadXML():void
{
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE,xmlHandler);
	loader.load( new URLRequest( "books.xml" ) );
}

function xmlHandler(e:Event):void
{
	try
	{
		trace(e.target.data);
	}
	catch(e:TypeError)
	{
		trace(e.message);
	}
}




Sending XML with AS3:

var xmlString:String = "<?xml version='1.0' encoding='ISO-8859-1'?><root><value>hello im xml</value></root>";
var testXML:XML = new XML(xmlString);

function sendXML():void
{
	var request:URLRequest = new URLRequest("yourscript.php");
	request.data = testXML;
	request.contentType = "text/xml";
	request.method = URLRequestMethod.POST;

	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, xmlHandler);
	loader.load(request);
}




Reading Node and Attribute Values:

Find and Node values is very easy when you know the node names. You simply dot down to reach them. If more than one node exists you can specify by add an index using bracket notation. The below example show examples of this using the books.xml file.

function loadXML():void
{
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE,xmlHandler);
	loader.load( new URLRequest( "books.xml" ) );
}

function xmlHandler(e:Event):void
{
	try
	{
		var resultXML:XML = new XML(e.target.data);

		////Find and Reading Node Values By Node Name
		trace(resultXML.book[0]);  // displays everything inside first book node
		trace(resultXML.book[0].author[0]);  // displays author of first book
		trace(resultXML.book[1].author[0]);  // displays author of second book

		////Find and Reading Node Attribute Values By Node and Attribute Name
		trace(resultXML.book[0].attribute("id")); // displays id attribute of first book

	}
	catch(e:TypeError)
	{
		trace(e.message);
	}
}


Here is the XML file I am working with:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are
      integrated into a comprehensive development
      environment.</description>
   </book>
</catalog>

Flash / Actionscript

  1.  
    No comments yet.
     
Post a Comment
  1. No trackbacks yet.
You must be logged in to post a comment.