Chapter 10. XML

XML IS AN IMPORTANT PART of dynamic ActionScript development, as it provides ways to define external data and simplify your ActionScript code. You can update XML content in a published Flash movie without republishing the Flash movie. XML data can also be dynamic, possibly even written based on database values.

With each new version of ActionScript, the XML object has been improved. While it existed in the original version of ActionScript, ActionScript 2 made it a native object. Now in ActionScript 3, the ECMAScript for XML (E4X) specification has been implemented, marking a giant step forward. E4X makes XML development consistent with the rest of the ActionScript syntax, providing a way to implement common operators, such as the dot (.) operator, to manipulate data.

ActionScript follows the ECMAScript for XML specification with the combination of the XML, XMLList, QName, and Namespace classes collectively known as E4X. This chapter covers some of the differences between the AS2 and AS3 versions of the XML object for tasks such as loading, event handling, and parsing.

Loading and Events

The XML object in ActionScript 2 is now equivalent to ActionScript 3’s XMLDocument. Those two are used in exactly the same way, so I won’t cover them here. Instead, this section focuses on the new ways you can use XML, and the differences between the old and new versions of the XML object.

Most of the time, using XML involves using external files that allow custom updates to be made to a published SWF file. The following code is an example of an XML file; we will load it to both ActionScript examples.

 1  <employees>
 2       <employee>
 3            <firstName><![CDATA[Jane]]></firstName>
 4            <lastName><![CDATA[Doe]]></lastName>
 5       </employee>
 6  </employees>

Note

employees is the root element. Numerous employee elements can exist as child elements. Text data within CDATA is not parsed by the XML parser and is used to prevent issues with special characters.

AS2: Loading and Events

In order for ActionScript to parse the data in an external XML document, the file must be represented as an XML object. Loading can proceed once an XML object has been instantiated, but make sure to ignore the white space in the document to prevent parsing issues.

The loading process begins with the load function, where you specify the path to the XML file that you want to load. In order to decipher when the XML file has loaded, the onLoad callback function is used. When used in a class, the onLoad function can use the Delegate to pass the event to a custom class method.

AS2: Loading and Events

AS3: Loading and Events

ActionScript 3’s XML object functions much differently than that of ActionScript 2. In AS3, an XML file must be officially loaded before the data can even be cast as (or converted to) an XML object.

The following code uses the same employee XML file sample, but the process of loading the file and passing the events is completely different. As covered in Chapter 9, loading is no longer handled by individual objects. Instead, all loading is handled by the URLLoader and URLRequest classes from the flash.net package. In order to initiate the loading of an XML file, the URLLoader must be instantiated, and a URLRequest must be passed to the load method of the URLLoader, to specify the path to the file being loaded. Event listeners are then added to dispatch custom class methods when different events occur, such as the loading progress or completion of an XML file.

In the example below, a custom class method named onXMLLoaded catches the completion event of the XML file. When onXMLLoaded is called, the event object that dispatched the method is passed as a parameter. This event is then used to retrieve the event target, which in this case is the URLLoader. Finally, the XML object can be instantiated based on the data contained in the URLLoader object.

In essence, this is the only time the XML object is used in this entire example; the rest of the code is all built around the loading process. The benefit of this approach is that the XML object is completely separated from the loading process, thus simplifying the code.

AS3: Loading and Events

Parsing

Parsing XML has always involved a lot of code writing to target appropriate nodes, attributes, and values. At last, ActionScript 3 has alleviated the pain of having to write so much code.

The following code serves as the XML file sample for both ActionScript examples in this section. The XML file has been updated to include multiple employees, each of which has an attribute for an id associated with his or her name.

Parsing

AS2: Parsing

As mentioned, parsing XML with ActionScript has always been a little tedious. The code in this example uses AS2 to parse the XML object when the XML file is completely loaded. In order to retrieve the few data items from the XML file, the employees element needs to be targeted and typed as an Array, then the Array of employees is iterated to target the id attribute. Within the first loop each employee element must be converted into an Array of its childNodes, and a second loop must be used to iterate the first and last name of each employee.

Figure . 

AS3: Parsing

AS3’s E4X makes XML parsing extremely easy, and far less code is needed to access specific values. In the following example, the dot (.) and attribute identifier (@) operators are used to target the elements and attributes in the XML object as if each element and attribute were native. Also, rather than using an Array to represent the employees, AS3 uses the XMLList class, which provides more methods that are not available to Array instances.

AS3: Parsing

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.216.249