Posted By

visualadvance on 04/07/09


Tagged

actionscript xml load 3 as3 loadXML


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

themill
mrbassman12
loric
sultano
vkord
jafar
ssolen
tspitzr


AS3: Load XML


 / Published in: ActionScript 3
 

  1. import flash.events.Event;
  2. import flash.net.URLLoader;
  3. import flash.net.URLRequest;
  4. import flash.display.Loader;
  5.  
  6. private var myXML:XML;
  7.  
  8. // pass the path to the xml, and the function to execute once it's loaded
  9. private function loadXML (path:String, nextFunc:Function):void
  10. {
  11. var loader:URLLoader = new URLLoader ();
  12. var req:URLRequest = new URLRequest (path);
  13. loader.addEventListener (ProgressEvent.PROGRESS, progressHandler, false, 0, true);
  14. loader.addEventListener (Event.COMPLETE, nextFunc, false, 0, true);
  15. loader.load (req);
  16. }
  17.  
  18.  
  19. private function progressHandler (evt:Event):void
  20. {
  21. var percent:Number = evt.currentTarget.bytesLoaded / evt.currentTarget.bytesTotal;
  22. trace (percent);
  23. }
  24.  
  25. // the COMPLETE function that executes when the file is laoded
  26. private function myXMLLoaded (evt:Event):void
  27. {
  28. myXML = new XML(evt.target.data);
  29. // myXML.item[0]
  30. }
  31.  
  32. // call the file to load and the COMPLETE function for when its done
  33. loadXML ("myFile.xml", myXMLLoaded);

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: anonrab on April 20, 2013

You forgot to remove your event listeners after load compete.

You need to login to post a comment.