User Guide
Working with external data 377
As the following example shows, Loading XML from an external file is the same as loading
URLVariables. You can create a URLRequest instance and a URLLoader instance and use
them to download a remote XML document. When the file has completely downloaded, the
Event.COMPLETE event is dispatched and the contents of the external file are converted to an
XML instance, which you can parse using XML methods and properties.
package
{
import flash.display.Sprite;
import flash.errors.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class ExternalDocs extends Sprite
{
public function ExternalDocs()
{
var request:URLRequest = new URLRequest("http://
www.[yourdomain].com/data.xml");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
try
{
loader.load(request);
}
catch (error:ArgumentError)
{
trace("An ArgumentError has occurred.");
}
catch (error:SecurityError)
{
trace("A SecurityError has occurred.");
}
}
private function completeHandler(event:Event):void
{
var dataXML:XML = XML(event.target.data);
trace(dataXML.toXMLString());
}
}
}