Posts Tagged ‘xml’

Loading XML with ActionScript and Arbol Negro

Thursday, May 27th, 2010

One of the most common task in actionscript in loading XML, it is the native way to load complex data; but there is a time when this process become a little anoying, because as you remember, in as2, load XML was just a couple of lines; in as3 is not that hard, but there are a couple of separate steps to load it and it’s sometimes confusing for begginers.

I have created XMLHandler to carry on those repetitions to “the easy way”, here is an example:


package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import net.absulit.arbolnegro.data.XMLHandler;
/**
* ...
* @author Sebastián Sanabria Díaz
*/
public class Main extends MovieClip
{

private var _xmlHandler:XMLHandler
public function Main()
{
_xmlHandler = new XMLHandler();
_xmlHandler.addEventListener(Event.COMPLETE, onCompleteXMLHandler);
_xmlHandler.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);
_xmlHandler.path = "data.xml";
}

private function onProgressEvent(e:ProgressEvent):void
{
trace(e.bytesLoaded / e.bytesTotal);
}

private function onCompleteXMLHandler(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, onCompleteXMLHandler);
//trace(e.target.xml);//using event parameter
trace(_xmlHandler.xml);//using the private var
}

}

}

This is a very simple example, loads XML, traces it, also shows its progress, which in a bigger XML file will be more useful.

Download example files:

Example-XMLHandler.zip

Switch to our mobile site