Chapter 15. E4X (XML)


In This Chapter

Creating XML Objects 239

Property Accessors 240

XML Filtering 241

Iterating Through an XMLList 242

Namespaces 243

Sending and Loading XML Data 243

Simple Soap Example 243

Summary 249

XML got a major overhaul with the release of ActionScript 3.0. The much-anticipated E4X has finally made its way into Flash Player 9. This makes working with XML in Flash much simpler and reduces the need for complex XML logic. Nearly everything you know about working with XML in Flash has changed with this release.

In this chapter, we examine the new features of E4X, look at how loading external XML files has changed, review the classic XML API, and create an example application that uses a few of these advanced features.

E4X stands for ECMAScript for XML. ECMAScript is the language ActionScript is based on. The E4X specification was developed by ECMA International and was released in the summer of 2004. The goal of E4X is to simplify the way developers use XML inside ECMAScript languages. It also introduces features that were not available in ActionScript 2.0.

New Classes in Actionscript 3.0

Because E4X is not simply an upgrade of the previous XML functionality, the class structure is not exactly the same. ActionScript 3.0 has the following top-level classes for working with E4X functionality: XML, XMLList, QName, and Namespace.

XML

The new XML class is nothing like the XML class in ActionScript 2.0. ActionScript 2.0 had two classes that represented the XML structure: XML and XMLNode. The XMLNode class was always a little silly because technically each node is a self-contained XML document. Therefore, in ActionScript 3.0, Adobe corrected this confusion by removing the XMLNode class. Now the XML type represents all elements, attributes, processing instructions, and text.

XMLList

ActionScript 3.0 introduces a new class called XMLList. In ActionScript 2.0, you could get an array of XMLNode objects by calling the childNodes property of any XMLNode object. Now, instead of working with a generic Array, you can use the XML.children() method to return an XMLList with the same functionality. A nice feature of the XMLList class is that if you have a case where there is only one XML object in the list, the XMLList delegates calls to that object. It basically functions as though it is that XML object. In the following example, lines 2 and 3 are identical because there is only one item:

var data:XML = <data><item>Test Value</item.</data>; var value1:String = data.item[0].toString(); var value2:String = data.item.toString();


The first line in the preceding example might seem a bit strange if you are completely new to E4X. It is a new way to define XML objects in ActionScript 3.0 using literals. We'll talk more about literals later in this chapter.

QName

The QName class, also new to ActionScript 3.0, is used to represent the qualified names of elements and attributes in an XML object. There are two parts to qualified names: The first part is a local name that conforms to XML standards, and the second part is a namespace URI (Uniform Resource Identifier). The namespace is optional.

Namespace

The Namespace class is used to define XML namespaces. Namespace support is a new feature included in E4X that is also used to define namespaces in your code not related to XML. It's important to understand that this class has multiple uses, only one of which relates to XML.





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net