AS2 → AS3: Load An XML File

Download Example Files

Loading XML in AS3 requires the use of the new Event system. In AS2 you could just tell the XML object to perform a method onLoad (when the XML file has finished loading and is ready to be parsed). In AS3 you have to add a listener to the Event.COMPLETE event and call your function to parse the XML after that event is fired off.

Here is the AS2 code:

  1. var XML:XML = new XML();
  2. XML.ignoreWhite = true;
  3. XML.onLoad = traceComplete;
  4. XML.load("data.xml");
  6. function traceComplete():Void
  7. {
  8.     trace("XML LOADED!");
  9. }

And here is the AS3 version:

  1. var xmlURL:String = "data.xml";
  2. var xmlLoader:URLLoader = new URLLoader();
  4. xmlLoader.addEventListener(Event.COMPLETE, traceComplete);
  5. xmlLoader.load(new URLRequest(xmlURL));
  7. function traceComplete($e:Event):void
  8. {
  9.     trace("XML LOADED!");
  10. }

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

No Comments

No comments yet.

Leave a comment