Posted By

orazal on 07/08/09


Tagged

xml loader as3


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

hasantayyar
liamr
KDR
jslice390
ClayVillage
ssolen


XML Loader


 / Published in: ActionScript 3
 

  1. import flash.events.Event;
  2. import flash.events.EventDispatcher;
  3. import flash.events.HTTPStatusEvent;
  4. import flash.events.IEventDispatcher;
  5. import flash.events.IOErrorEvent;
  6. import flash.events.ProgressEvent;
  7. import flash.events.SecurityErrorEvent;
  8. import flash.net.URLLoader;
  9. import flash.net.URLLoaderDataFormat;
  10. import flash.net.URLRequest;
  11.  
  12. var dataXML:XML;
  13. var path = "test.xml";
  14. var xmlRequest:URLRequest = new URLRequest(path);
  15. var xmlLoader:URLLoader = new URLLoader()
  16. xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
  17. configXmlLoaderListeners(xmlLoader);
  18. xmlLoader.load(xmlRequest);
  19.  
  20.  
  21.  
  22. //---------------------------------
  23. //
  24. // Listeners
  25. //
  26. //--------------------------------
  27.  
  28. function configXmlLoaderListeners(dispatcher:IEventDispatcher):void {
  29. dispatcher.addEventListener(Event.COMPLETE, xmlCompleteHandler);
  30. dispatcher.addEventListener(Event.OPEN, xmlOpenHandler);
  31. dispatcher.addEventListener(ProgressEvent.PROGRESS, xmlProgressHandler);
  32. dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, xmlSecurityErrorHandler);
  33. dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, xmlHttpStatusHandler);
  34. dispatcher.addEventListener(IOErrorEvent.IO_ERROR, xmlIoErrorHandler);
  35. }
  36.  
  37. function xmlCompleteHandler(event:Event):void {
  38. var loader:URLLoader = URLLoader(event.target);
  39. //trace("completeHandler: " + loader.data);
  40.  
  41. try{
  42. dataXML = new XML(event.target.data)
  43. //trace("dataXML " + dataXML);
  44.  
  45. gotoAndStop("build menu");
  46.  
  47. } catch (error:TypeError){
  48. trace("Could not parse the XML")
  49. trace(error.message)
  50. }
  51. }
  52.  
  53.  
  54. // Optional listeners
  55.  
  56. function xmlOpenHandler(event:Event):void {
  57. trace("openHandler: " + event);
  58. }
  59.  
  60. function xmlProgressHandler(event:ProgressEvent):void {
  61. trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
  62. }
  63.  
  64. function xmlSecurityErrorHandler(event:SecurityErrorEvent):void {
  65. trace("securityErrorHandler: " + event);
  66. }
  67.  
  68. function xmlHttpStatusHandler(event:HTTPStatusEvent):void {
  69. trace("httpStatusHandler: " + event);
  70. }
  71.  
  72. function xmlIoErrorHandler(event:IOErrorEvent):void {
  73. trace("ioErrorHandler: " + event);
  74. }

Report this snippet  

You need to login to post a comment.