Using the XML Object


Nearly everything you do with XML in Flash involves the XML object and falls into one of the following categories:

  • Formatting XML

  • Parsing XML (extracting the information)

  • Loading XML

  • Sending XML

With the XML object you will be able to load XML from an external source such as a static file or a server-side script. Once this XML document is loaded, you can access its information using the methods and properties of the XML object. Also using the methods and properties of the XML object, you can create your own XML document. Once this document is created, you can use it in your Flash movie or send it out to a server-side script. All the ActionScript needed to do these things is covered in this section.

Formatting XML

The XML object in Flash has several methods, all of which you can use to create and format XML documents. Truth is, though, you're unlikely to employ them because they're difficult to use and there's a better way! We'll show you how to create a string and then convert it into an XML object a much easier (and more common) way of formatting XML objects.

To create an XML object in Flash, you must use its constructor. Here is how you create an empty XML object:

 myXML = new XML();  

To populate the object with XML-formatted data when it is created, you can pass (inside the parentheses of the constructor) the name of a variable that holds an XML-formatted string or another XML object.

If, in Flash, we wanted to create the following XML document:

 <MyFriends>      <Name Gender="female">Kelly Makar</Name>     <Name Gender="male">Free Makar</Name> </MyFriends> 

we would do two things: create the document as a string, and convert the string to an XML object using the XML object constructor, new XML().

Here is an example:

 myString = "<MyFriends><Name Gender=\"female\">Kelly   Makar</Name><Name Gender=\"male\">Free  Makar</Name></MyFriends>"; myXML = new XML(myString); 

The above ActionScript creates the XML document as a string and then converts it to an actual XML object called myXML. This object can then be sent to the server using the send-related methods described later in this section.

Parsing XML

The word parse simply means to analyze something or break it down into its parts. Thus, when someone speaks of writing a script to parse an XML document, he or she is talking about writing a script that extracts information from that XML document. The XML object has many properties to help you do this. We'll use the XML object you created in the previous subsection, myXML, to illustrate the use of a few of the most common properties.

firstChild

This property points to the first node in the tree structure. For example, myXML.firstChild.firstChild returns the following:

 <Name Gender="female">Kelly Makar</Name>  

The first child node of the XML document is the root node (MyFriends), and the root node's first child is Name, as shown.

childNodes

This property returns an array of the child nodes at any given point in the tree structure. For instance:

 myArray = new Array()  myArray = myXML.firstChild.childNodes 

Here, myArray contains two elements whose values are the same as those of the two Name nodes.

nextSibling

This property points to the next node in the same level of the tree structure. For instance, myXML.firstChild.firstChild.nextSibling returns the following:

 <Name Gender="male">Free Makar</Name>  
attributes

This property returns an associative array of attribute names. Thus, myXML.firstChild.firstChild.nextSibling.attributes.Gender returns "male".

graphics/dfig04.gif

The list above includes the most commonly used properties of the XML object; others work in the same way, referencing different parts of the tree structure.

Loading XML

Typically, you'll only work with XML in Flash when you're loading it or sending it out. To load XML from a remote source, you do the following:

  1. Create an XML object.

  2. Use the load() method of the XML object to load XML-formatted data from an external source.

For example:

 myXML = new XML();  myXML.load("http://somedomain.com/info.xml"); 

As the example shows, this URL does not need to point to a static XML file. It can point to an ASP (or another scripted page) whose result is an XML document.

It's easy to determine when the XML has finished loading into an object by using the onLoad event available to the XML object. You can define this event to call a function when the document is finished loading. Take a look at the following example:

 function init () {     //parse script here } myXML = new XML(); myXML.onLoad = init; myXML.load("../somedomain.com/info.xml"); 

As the next-to-last line shows, when the XML document is finished, loading the init function will be called.

Sending XML

The XML object allows you to send XML to a URL. It also lets you send XML and load the resulting document simultaneously.

To send XML to a URL, use the send() method and specify a destination URL. For instance:

 myXML = new XML("<Message><Text>Hi!</Text></Message>");  myXML.send("http://somedomain.com/somedestination.asp"); 

To send XML and receive a response, all in one shot, use the sendAndLoad() method of the XML object. With this method, you must specify an XML object whose contents you wish to send, a URL in which to send the XML document, and an XML object in which to receive the response. As shown with the load() example in the previous section, you must define an onLoad event to handle the loaded XML. Here is an example:

 URL = "http://www.electrotank.com/projects/tfts/using_xml/   UserLogin.asp"; function init () {     trace(objToReceive); } xmlToSend = "<Login><UserName>Jobem</UserName><Password>hayes  </Password></Login>"; objToSend = new XML(xmlToSend); objToReceive = new XML(); objToReceive.onLoad = init; objToSend.sendAndLoad(URL, objToReceive); 

graphics/dfig05.gif

The above ActionScript creates an XML object (objToSend) containing log-in information, and then sends that information to a URL where it waits for a response from the destination. When the response is fully loaded into the receiving XML object (objToReceive), the init function is called.



Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

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