Easy XML Loading util

Submitted by:Jhon Brain

Date added:22 February, 2011

Category:ActionScript 3

Simple usage:

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

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

Tags: xml loading util , xml loading script

Code Snippet:

   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. }
 
 

Comments