User Guide
Working with external data 375
Once the data has loaded, the Event.COMPLETE event is dispatched, and the contents of the
external document are available to use in the URLLoader’s
data property, as the following
code shows:
private function completeHandler(event:Event):void
{
var loader2:URLLoader = URLLoader(event.target);
trace(loader2.data);
}
If the remote document contains name-value pairs, you can parse the data using the
URLVariables class by passing in the contents of the loaded file, as follows:
private function completeHandler(event:Event):void
{
var loader2:URLLoader = URLLoader(event.target);
var variables:URLVariables = new URLVariables(loader2.data);
trace(variables.dayNames);
}
Each name-value pair from the external file is created as a property in the URLVariables
object. Each property within the variables object in the previous code sample is treated as a
string. If the value of the name-value pair is a list of items, you can convert the string into an
array by calling the
String.split() method, as follows:
var dayNameArray:Array = variables.dayNames.split(",");
Instead of loading the contents of the remote file as a string and creating a new URLVariables
object, you could instead set the
URLLoader.dataFormat property to one of the static
properties found in the URLLoaderDataFormat class. The three possible values for the
URLLoader.dataFormat property are as follows:
■ URLLoaderDataFormat.BINARY—The URLLoader.data property will contain binary
data stored in a ByteArray object.
■ URLLoaderDataFormat.TEXT—The URLLoader.data property will contain text in a
String object.
■ URLLoaderDataFormat.VARIABLES—The URLLoader.data property will contain URL-
encoded variables stored in a URLVariables object.
TIP
If you are loading numeric data from external text files, you need to convert the values
into numeric values by using a top-level function, such as
int(), uint(), or Number().