Created
July 21, 2013 07:04
-
-
Save RKAN/6047781 to your computer and use it in GitHub Desktop.
Basic external XML loader / Published in: ActionScript 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment