20.0. IntroductionXML is a structured, text-based way of formatting and describing data. It was originally designed to be to both simple and flexible and has rapidly grown into an industry standard because of its portability, especially for data exchange and interoperability between applications. When working in ActionScript, XML will probably cross your path. Chapter 19 explains how to send and load data in a URL-encoded format. URL-encoding is fine for passing simple data between the Flash Player and server-side scripts, but for complex data or Unicode characters, XML generally works much better because of its structured format. 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 URLLoader instance: myString=a+string+value 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 (|): myObject=prop0|val0*prop1|val1*prop2|val2 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:
Of course, XML isn't the only way of transferring data in and out of the Flash Player. Chapters 19, 21, and 24 discuss ways to communicate outside of the Flash Player as well. However, this chapter focuses solely on XML, an industry standard technique for exchanging data that doesn't require the use of additional server-side software (as Flash Remoting and Sockets do). XML has become an important part of ActionScript 3.0 and has been given special treatment this time around. ActionScript 3.0 boasts a revolutionary new syntax for working with XML. ECMAScript for XML, otherwise known as E4X, is a language extension that gives you a simpler, easier to read approach for working with XML objects than the traditional Document Object Model (DOM), an interface of the past. Using E4X, you'll find that you can work with XML much easier than before. Additionally, if this is your first time working with XML, E4X dramatically lowers the learning curve of using XML. In this chapter, the following terminology is used:
|