10-17-2008
Actionscript 3: Loading an XML File
XML files allow you to keep all of your data outside of your source .fla, giving you complete dynamic control of the content without having to go republish your flash file. Since most of the work I do involves an XML file, this is how i use the URLLoader Class to load the data:
private function loadXml():void
{
var xmlLoader:URLLoader = new URLLoader();
try
{
xmlLoader.load(new URLRequest("path to xml"));
}
catch(e:Error){}
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, io, false, 0, true);
xmlLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded, false, 0, true);
}
private function loadProgress(e:ProgressEvent):void
{
var percent:String = Math.round(e.bytesLoaded / e.bytesTotal) * 100 + "% loaded";
}
private function xmlLoaded(e:Event):void
{
e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, io);
e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
e.currentTarget.removeEventListener(Event.COMPLETE, xmlLoaded);
_xml = new XML(e.target.data);
}
private function io(e:IOErrorEvent):void{}
this will work in most all instances, it does for me.