AS3 Load External XML File


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

Here is a Document Class that loads an external XML file.


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.net.URLLoader;
  7. import flash.net.URLRequest;
  8. import flash.net.URLLoaderDataFormat;
  9. import flash.system.Capabilities;
  10. import flash.text.TextField;
  11. import flash.text.TextFormat;
  12.  
  13. public class Main extends Sprite
  14. {
  15.  
  16. private var _xmlPath:String = "data.xml";
  17. private var _xmlHasLoaded:Boolean = false;
  18.  
  19. private var _xmlLoader:URLLoader;
  20. private var _xmlRequest:URLRequest;
  21. private var _dataXml:XML;
  22. private var _id:String;
  23. private var _title:String;
  24.  
  25. public function Main():void
  26. {
  27. if (stage) init();
  28. else addEventListener(Event.ADDED_TO_STAGE, init);
  29. }
  30.  
  31. private function init():void
  32. {
  33. removeEventListener(Event.ADDED_TO_STAGE, init);
  34. loadDataXml(_xmlPath);
  35. }
  36.  
  37. private function loadDataXml($xmlPath:String):void
  38. {
  39. trace("loadDataXml("+$xmlPath+")");
  40. _xmlLoader = new URLLoader();
  41. _xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
  42. _xmlLoader.addEventListener(Event.COMPLETE, onXmlLoader_COMPLETE);
  43. _xmlLoader.addEventListener(IOErrorEvent.IO_ERROR , onXmlLoader_IO_ERROR);
  44. _xmlRequest = new URLRequest();
  45.  
  46. if (Capabilities.playerType == "External") {
  47. _xmlRequest.url = $xmlPath;
  48. } else {
  49. _xmlRequest.url = $xmlPath + "?rand=" + Math.random();
  50. }
  51.  
  52. try {
  53. trace("try: urlLoader.load("+_xmlRequest.url+")");
  54. _xmlLoader.load(_xmlRequest);
  55. } catch (error:Error) {
  56. trace("catch: Unable to load XML data.");
  57. }
  58. }
  59.  
  60. private function onXmlLoader_COMPLETE(event:Event):void
  61. {
  62. trace("onXmlLoader_COMPLETE()");
  63. _xmlLoader.removeEventListener(Event.COMPLETE, onXmlLoader_COMPLETE);
  64. _xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR , onXmlLoader_IO_ERROR);
  65. _xmlHasLoaded = true;
  66.  
  67. try {
  68. _dataXml = new XML(event.target.data);
  69. trace(_dataXml.toString());
  70. start();
  71. } catch (err:TypeError) {
  72. trace("Could not parse text into XML");
  73. trace(err.message);
  74. }
  75. }
  76.  
  77. private function onXmlLoader_IO_ERROR(event:IOErrorEvent):void
  78. {
  79. trace("onXmlLoader_IO_ERROR("+event+")");
  80. trace("event.text: "+String(event.text));
  81. }
  82.  
  83. private function start():void
  84. {
  85. // Do something here
  86. _id =_dataXml.title.@id;
  87. trace("_id: "+_id);
  88. _title = _dataXml.title;
  89. trace("_title: "+_title);
  90.  
  91. var tfmt:TextFormat = new TextFormat();
  92. tfmt.size = 22;
  93. tfmt.font = "Arial";
  94.  
  95. var tf:TextField = new TextField();
  96. tf.width = 549;
  97. tf.height = 399;
  98. tf.wordWrap = true;
  99. tf.multiline = true;
  100. tf.defaultTextFormat = tfmt;
  101. tf.condenseWhite = false;
  102. tf.border = true;
  103. tf.htmlText = _title;
  104. addChild(tf);
  105. }
  106.  
  107.  
  108. }
  109. }
  110.  
  111. ////////////////////////////////////////////////////////
  112. // The XML file in this situation would
  113. // look like this and be called 'data.xml'
  114. //
  115. // <?xml version="1.0" encoding="utf-8"?>
  116. // <root>
  117. // <title id="001"><![CDATA[Hello World]]></title>
  118. // </root>
  119. //
  120. ////////////////////////////////////////////////////////

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.