The capability to load, parse, and send XML data was added to Flash in version 5, and it marked a significant advance in the ease of communicating complex data to and from a server script. Prior to the introduction of the XML class to Flash, the only way to load and send text data was to use URL-encoded values, as discussed in Chapter 18. XML is a superior alternative in many cases because, unlike URL-encoded data, XML data is structured. URL encoding is fine for passing simple data between Flash and server-side scripts, but for complex data or Unicode characters, XML generally works much better. For example, if you want to load data from a text file that represents a simple datatype such as a string, URL-encoded data, such as the following, can be loaded using a LoadVars object:
However, when you want to load data from an external source and use that data to create an ActionScript object, you are presented with the problem of how to represent that data as a URL-encoded string. You might try something like the following, in which each property-value pair is separated by an asterisk (*), and each property is separated from its corresponding value by a vertical pipe (|):
Once the string value is returned for myObject, you could use String.split( ) to recreate the elements that make up the object. Although you can get by with this approach, it is often much easier to represent complex values in XML. For example, the same object can be represented by the following XML snippet:
<myObject> <prop0>val0</prop0> <prop1>val1</prop1> <prop2>val2</prop2> </myObject>
XML data offers several advantages over URL-encoded data, including:
There are, of course, other ways to send and receive data in Flash. Chapter 17 covers ways to transmit data using the LocalConnection and SharedObject classes. Chapter 20 covers data transmission using Flash Remoting. However, this chapter focuses on XML, which doesn't require a server-side installation (as does Flash Remoting) and works in Flash Player 5, unlike the LocalConnection and SharedObject classes.
An XML tree generally has an object of the XML class as its top node and objects of the XMLnode class as nodes below it. Many of the methods and properties discussed in this chapter work with both XML and XMLNode objects, but some apply to objects of the XML class only. This is because the XML class is a subclass of the XMLNode class.
The following methods and properties apply to all instances of the XMLNode and XML classes:
The following methods, properties, and event handlers apply to instances of the XML class but not to XMLNode instances:
In this chapter, the following terminology is used: