Previous Page
Next Page

20.0. Introduction

XML 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:

  • When creating XML manually (for a static XML document) or programmatically (from a ColdFusion script, PHP script, etc.), it is much easier to represent complex data.

  • Most server-side scripting languages offer built-in functionality for reading and generating XML data.

  • XML is a standard used for the transfer and storage of data across all kinds of applications and platforms.

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:



XML document

A file that contains XML. This term may also refer to XML data that is being loaded or sent. An XML document is not to be confused with the XMLDocument class.



XML packet

An XML packet can be any snippet of XMLfrom an entire XML document to just a single nodeas long as it is represents a complete, well-formed piece of information in XML.



XML node

The basic building block for XML. Nodes can be elements, text nodes, attributes, and so on. We refer to elements and text nodes collectively as "nodes" when talking in general terms.



XML element

The term "element" is often used interchangeably with the term "tag." More accurately, however, an element contains tags. Elements must have an opening and closing tag (<element></element>), or the opening and closing tags can be combined into one when the element has no nested elements (<element />).



Root node

An element that is at the top of the XML hierarchy of elements.



Text node

A node containing text. Text nodes are generally nested within elements.



Attribute

Is part of an element. Attributes are placed within the tags of elements in name/value pair format, such as <element name="value">.



XML declaration

The declaration typically looks like this: <?xml version="1.0" ?>. This is a special tag that the XML parser recognizes as containing information about the XML document, and it is not parsed as an element.



XML tree

Also sometimes called the "data tree," an XML tree is the hierarchy of nodes in XML data.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)