/ Published in: ActionScript 3
Loads a specified XML file provided in the class constructor. Dispatches an event when the XML is loaded and ready.
Expand |
Embed | Plain Text
package { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; /** * ... * @author ... */ public class LoadXML extends EventDispatcher { private var data:XML; private var loader:URLLoader; public function LoadXML(path:String) { loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true); loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true); loader.load(new URLRequest(path)); } //--------------------------------------------------------------------------------------- private function onComplete(event:Event):void { try { data = new XML(event.target.data); loader.removeEventListener(Event.COMPLETE, onComplete); loader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError); dispatchEvent(new Event(Event.COMPLETE)); } catch (error:Error) { trace("Could not load XML: " + error); } } //--------------------------------------------------------------------------------------- private function onIOError(event:IOErrorEvent):void { trace("An error occured trying to load the XML: " + event.text); } //--------------------------------------------------------------------------------------- public function getXML():XML { return data; } //--------------------------------------------------------------------------------------- } } //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //ADD THIS TO THE TIMELINE import flash.events.Event; var n:LoadXML = new LoadXML("data.xml"); var xml:XML; n.addEventListener(Event.COMPLETE, onComplete); function onComplete(event:Event):void { xml = n.getXML(); }
You need to login to post a comment.
