The XML Object

I l @ ve RuBoard

Flash's XML object is a collection of functions and properties that allow you to easily get data from XML documents. The first step in using the XML object is to create a variable that holds XML. Here is an example:

 myXML = new XML(); 

Parsing Text into XML

This XML document is empty. However, you can quickly populate it by using the parseXML command. This takes a text string and deconstructs it to get the XML data inside.

 myXML = new XML(); myXML.parseXML("<user><name>Gary</name><ID>47</ID></user>"); 

Another way to do this same thing is to feed the string into the new XML() function:

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); 

Now just because you say that a string represents XML doesn't make it true. Suppose that there is a mistake in your XML code. In that case, ActionScript won't be able to parse the string. You can find out whether there was a problem by monitoring the status property of the object. Here is an example:

 myXML = new XML("<user><name>Gary</name><ID>47</user>"); trace(myXML.status); 

The result is -9. This is because the closing </ID> tag is missing. The code -9 translates to a start tag without an end tag. A -10 is an end tag without a start tag. But if you get a 0, that means that the string was parsed without any problems.

Getting Data from XML

So how do you access the information inside the XML object now that you have it? There are many functions for doing this. For instance, you can use firstChild to get the first child node of the object:

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.firstChild); 

The result is "<user><name>Gary</name><ID>47</ID></user>" . This is the first child of the XML object. Now let's dig deeper:

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.firstChild.firstChild); 

Now the result is <name>Gary</name> . The name element is the first child of the user element.

Another way to do this, instead of using firstChild , is to use childNodes , an array of nodes. Here is the same example, but with childNodes :

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.childNodes[0].childNodes[0]); 

Is that it? No, you can still go further. Refer back to Figure 19.1. There is a node under the user node that holds the text.

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.childNodes[0].childNodes[0].childNodes[0]); 

The result is now Gary . This is the text node of the name node of the user node. So that's the bottom? No. This is the last node, but it is still a node, not a text string like we would probably need if we were looking for the data in the XML document. To access the string in a text node, you need to use the nodeValue property. Here is the way to get the username from the XML document:

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.childNodes[0].childNodes[0].childNodes[0].nodeValue); 

But what if we wanted the user ID? That would be the second node of the user node. Here is the code:

 myXML = new XML("<user><name>Gary</name><ID>47</ID></user>"); trace(myXML.childNodes[0].childNodes[1].childNodes[0].nodeValue); 

Creating XML from Scratch

What if you didn't have a text string but wanted to create the XML code from scratch? You can use createElement to make a new element and createTextNode to create a new text node. However, neither of these commands actually adds the node to the XML object; they just prepare a new node. You have to use appendChild to add a new node.

For instance, to re-create the XML object from the previous example, you would do this:

 myXML = new XML(); newElement = myXML.createElement("user"); myXML.appendChild(newElement); newElement = myXML.createElement("name"); myXML.childNodes[0].appendChild(newElement); newText = myXML.createTextNode("Gary"); myXML.childNodes[0].childNodes[0].appendChild(newText); newElement = myXML.createElement("ID"); myXML.childNodes[0].appendChild(newElement); newText = myXML.createTextNode("47"); myXML.childNodes[0].childNodes[1].appendChild(newText); 

If you want to change the value of a text node, you need to set the nodeValue property:

 myXML.childNodes[0].childNodes[1].childNodes[0].nodeValue = 53; 

Attributes

XML elements can also have attributes. An attribute is a property and value pair that further defines an element. For instance, in the following XML, the attribute type helps to define the element name :

 <user>     <name type="alias">Gary</name>     <ID>47</ID> </user> 

If you wanted to read this into an XML object, you would have to use a single quote or a backslash followed by a quote, in place of the quotes around "alias" . Otherwise, the ActionScript line would have a syntax error.

 myXML = new XML("<user><name type='alias'>Gary</name><ID>47</user>"); 

To access this attribute, you need to refer to the attributes property of the node and then the specific attribute name:

 trace(myXML.childNodes[0].childNodes[0].attributes.alias); 

Another way to do this uses brackets:

 trace(myXML.childNodes[0].childNodes[0].attributes["alias"]); 

You can set, change, or even add an attribute to a node like this:

 myXML.childNodes[0].childNodes[0].attributes["alias"] = "real"; 

If the attribute alias wasn't already there, it would be created.

Unfortunately, the attributes property is not an array. So you can't get its length or examine the property name and value of each element by number. You can, however, use a for in loop to discover each attribute.

 myXML = new XML("<user><name type='alias' validity='verified'>Gary</name> <ID>47</user>"); for(attr in myXML.childNodes[0].childNodes[0].attributes) {     trace(attr+": "+myXML.childNodes[0].childNodes[0].attributes[attr]); } 

More XML ActionScript

You should know several other things about the XML object. Most important, remember that any part of an XML object is another XML object. For instance, you can take the first node of an XML object and refer to it with another variable. Here is an illustration:

 myXML = new XML("<user><name>Gary</name><ID>47</user>"); thisUser = myXML.childNodes[0]; thisUserName = thisUser.childNodes[0]; thisUserNameText = thisUserName.childNodes[0].nodeValue; thisUserID = thisUser.childNodes[1]; thisUserIDText = thisUserID.childNodes[0].nodeValue; 

You can get the number of nodes inside a node by using the length property of the childNodes property of that node. So to find that the user node has two nodes inside it, you could do this:

 myXML = new XML("<user><name>Gary</name><ID>47</user>"); trace(myXML.childNodes[0].childNodes.length); 

In addition to being able to find out the nodeValue of a text node, you can find out the name of a regular element node with nodeNode . So you could determine that the first node under user is name with this:

 myXML = new XML("<user><name>Gary</name><ID>47</user>"); trace(myXML.childNodes[0].childNodes[0].nodeName); 

You can determine whether a node is an element or a text node by using the nodeType property. A value of 1 means that it is a regular element with possibly more nodes under it, or a value of 3 tells you that it is a text node.

Loading External XML

You can get XML files from an external source. The XML object has the same basic commands and properties as the LoadVars object in Hour 18, "Sending Information to the Server."

First, you would create the XML object. Then you would use the load command to initiate a server call. The results would happen some time later, which you can check for using the loaded property of the object or defining an onLoad function.

For instance, this code starts to load the XML document xmldata.txt from the same folder as the Flash movie. When it is finished, it calls the function dealWithXML :

 data = new XML(); data.ignoreWhite = true; data.load("xmldata.txt"); data.onLoad = function(success) {     if (success) {         if (data.status == 0) {             // take data from this user             dealWithXML(thisUser);         }  else {             // could not parse the XML data             trace("Parse Error: "+data.status);         }     }  else {         // could not get the XML         trace("Load Error");     } } 

A few more things in the preceding function need explaining. The ignoreWhite property of the XML object tells it to ignore whitespace: tabs, returns, line feeds and other nonvisible characters. This means that the XML file can have multiple lines and use tabs to make it easier to read. Flash ignores these characters when it parses the XML data. This is necessary when using human-created XML files, or even files generated by programs but meant to be examined by humans .

The onLoad function takes a single parameter that will be true if the load was a success. If not, there was an error, and the preceding function handles that case. In addition, I use the status property to make sure that Flash can parse the XML file.

Now that you know how to get an external XML file and manipulate it in a number of ways, let's create a simple XML display.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

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