Introduction

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:

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.

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:

appendChild( )
attributes
childNodes
cloneNode( )
firstChild
hasChildNodes( )
insertBefore( )
lastChild
nextSibling
nodeName
nodeType
nodeValue
parentNode
previousSibling
removeNode( )
toString( )

The following methods, properties, and event handlers apply to instances of the XML class but not to XMLNode instances:

contentType
createElement( )
createTextNode( )
docTypeDecl
getBytesTotal( )
getBytesLoaded( )
ignoreWhite
load( )
loaded
onData( )
onLoad( )
parseXML( )
send( )
sendAndLoad( )
status
xmlDecl

In this chapter, the following terminology is used:

XML document

A file containing XML, or the contents of that file. We also use this term to refer to XML data that is being loaded or sent.

XML packet

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

XML node

A node is 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 (<myElement></myElement>), or the opening and closing tags can be combined into one when the element has no nested elements (<myElement />).

Root node

The root node is 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

An attribute is a node that is part of an element. Attributes are placed within the tags of elements in name/value pair format, such as <myElement name="value">.

XML declaration

The declaration typically looks like this: <?xml version="1.0" ?>. It 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.



    Part I: Local Recipes
    Part II: Remote Recipes