2007年5月9日星期三

Loading Text and XML with URLLoader

In previous versions of ActionScript, there were a couple of classes who had the capability of loading external text, namely LoadVars and XML. The loading responsibilities of these classes has moved to one single class in ActionScript 3, URLLoader (flash.net.URLLoader). This class is a lot like LoadVars. The big difference is that it is used for XML since the responsibility of loading XML from an external source has been removed from the XML class. Instead, you would load the text with URLLoader and then give that text to an XML object for parsing.

Like LoadVars, URLLoader has a load() method that is used to load text from an external source. This accepts 1 argument, a URLRequest instance (NOT a URL string). You can then use events from URLLoader to determine when the loading is complete. When complete, the text loaded is available in the data property of URLLoader.

Example:

ActionScript Code:

var loader:URLLoader;
// ...
loader = new URLLoader();
loader.
addEventListener(Event.COMPLETE, xmlLoaded);

var request:URLRequest = new URLRequest(
"file.xml");
loader.
load(request);
//...
function xmlLoaded(event:Event):
void {
var myXML:
XML = new XML(loader.data);
//...
}

0 评论: