Easy XML Loading util


/ Published in: ActionScript 3
Save to your folder(s)

Simple usage:

var xmlLoader:LoadXMLUtil = new LoadXMLUtil("path_to_xml.xml");
xmlLoader.addEventListener(Event.COMPLETE, onLoaderComplete);

private function onLoaderComplete(event:Event):void{
trace("Loaded XML: ");
trace(xmlLoader.xml);
}


Copy this code and paste it in your HTML
  1. package utils {
  2.  
  3. /*
  4. Author: Bassta
  5. Simple XML Loadig util - takes on parameter in the constructur function;
  6. path_to_xml is String, which tells where the xml file is
  7. Dispatches Event.COMPLETE when the xml is loaded
  8. you can retrive the XML by using "xml" getter function
  9. */
  10.  
  11. import flash.events.*;
  12. import flash.net.*;
  13.  
  14. public class LoadXMLUtil extends EventDispatcher{
  15.  
  16. private var _source:XML;
  17. private var loader:URLLoader = new URLLoader();
  18.  
  19. public function LoadXMLUtil(path_to_xml:String){
  20. loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
  21. loader.load(new URLRequest(path_to_xml));
  22. }
  23.  
  24. internal function onComplete(event:Event):void{
  25. loader.removeEventListener(Event.COMPLETE, onComplete);
  26. XML.ignoreWhitespace = true;
  27. _source = new XML(event.target.data);
  28. dispatchEvent(event.clone());
  29. }
  30.  
  31. public function get xml():XML{
  32. return _source;
  33. }
  34.  
  35.  
  36. }//end
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.