AS 3.0 – The new way to load XML
26th October 2005 | 0 Comments
I’ve been dabbling with some of the new features of Flex Builder 2.0. Overall, I’m pretty impressed with the quality and power. ActionScript 3.0 should finally quell any last remaining doubts that ActionScript is a real programming language.
One of the new classes found in ActionScript 3.0, is the URLLoader class. This class replaces the object specific load methods. You no longer call the load method from an XML object, instead you have URLLoader load the data and pass it to the XML object.
A basic implementation of the URLLoader class follows.
package {
import flash.display.MovieClip; // Ye good olde MovieClip class
import flash.xml.*; // XML related classes
import flash.net.*; // URL Loader and Request classes
import flash.events.*; // Allow handling of events
public class LoaderDemo extends MovieClip {
private var myLoader:URLLoader;
public function LoaderDemo () {
this.myLoader = new URLLoader();
this.myLoader.addEventListener("complete",this.XMLLoaded);
this.myLoader.load(new URLRequest("demo.xml"));
}
private function XMLLoaded(evt:Event) {
var myXML:XML = new XML(this.myLoader.data);
}
}
}
In the Flex Builder 2.0 IDE, I created a project called LoaderDemo. It created the basic structure of the ActionScript file. From there you create a URLLoader object, assign a "complete" listener, and tell it which file to load (via URLRequest). When the file is done loading it calls the function referenced in the "complete" listener and from there you can do whatever you want with the data. Yes, its different from previous versions of ActionScript, but now you can use the same code to load just about everything. There is even a streaming version of this class, which lets you work with data as it downloads.
Leave a Comment