Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 289 290 291 292 293 294 < 295 > 296 297 298 299 300 301 .. 419 >> Next

Reading Element Information
All tags and text are considered to be nodes in XML. Thus, two types of nodes can be parsed into an XML object: tags (or tag elements) and text. Each of these different types has an ID.
Tag elements have an ID of 1, whereas text nodes have an ID of 3. Every XMLNode object has a nodeType property that reveals what type of node you are dealing with. Listing 26-5 shows a few examples of reading the nodeType property for different elements.
Listing 26-5: Reading the nodeType Property
var sXml:String = "<book><author>Mark Twain</author>^
<title>Huckleberry Finn</title></book>";
var xmlVal:XML = new XML(sXml);
var xnRoot:XMLNode = xmlVal.firstChild;
trace(xnRoot.nodeType);
var xnAuthorTag:XMLNode = xnRoot.firstChild; trace(xnAuthorTag.nodeType);
var xnAuthorName:XMLNode = xnAuthorTag.firstChild; trace(xnAuthorName.nodeType);
It writes the following to the Output panel:
^Note
When a node has a nodeType value of 1, the nodeName has the value of the tag name. For instance, the following code appended to the code from Listing 26-5:
trace(xnRoot.nodeName);
trace(xnAuthorTag.nodeName);
writes the following to the Output panel:
book
author
However, when a node has a nodeType value of 3, the nodeName has a null value: trace(xnAuthorName.nodeName); and results in the following in the Output panel:
null
So, how do you retrieve the value of a text node? There is yet another property of XMLnode objects that contains the value of the text node. Therefore, this property is null for all objects with a nodeType of 1. For objects with a nodeType of 3, the nodeValue contains the text value of the following node:
Whitespace nodes such as carriage returns and other special characters are treated as text nodes (nodeType of 3).
644
Part VII ¶ The Data Classes
trace(xnAuthorName.nodeValue); which results in the following in the Output panel:
Mark Twain
Building a Document Tree
You can construct your own XML objects and build their document trees within ActionScript based on environment and user data. You have seen how to use the constructor function and the parseXML() method to parse a string into the objectís document tree. But it can also be convenient to be able to build the document tree node by node. The methods of the XML object enable you to do exactly this.
Writing the XML Declaration and DTD
The xmlDecl and docTypeDecl properties are read-write properties, and you can therefore use these properties to set the values of either the XML declaration or the DTD:
var xmlVal:XML = new XML(); xmlVal.xmlDecl = "<?xml version=\"1.0\"?>";
xmlVal.docTypeDecl = "\"<!DOCTYPE library[<!ELEMENT book (#PCDATA)>]>";
Creating Nodes
Once youíve created a new XML object you next want to create one or more new nodes to add to its document tree. Youíll recall that all nodes are instances of the XMLNode class. Therefore, in order to create these nodes, you can use the XMLNode constructor. The XMLNode constructor allows you to create either tag nodes or text nodes by specifying the type ID of 1 or 3, respectively. Listing 26-6 shows an example of each.
Listing 26-6: Making New XMLNode Objects
var xnNewElement:XMLNode = new XMLNode(1, "author"); var xnNewTextNode:XMLNode = new XMLNode(3, "Mark Twain");
Thatís all there is to it. You now have a new element, <author>, assigned to the variable xnNewElement. And you have a new text node, Mark Twain, assigned to the variable xnNewTextNode. The only catch is that the nodes donít have any parent, children, or siblings. In other words, the nodes do not have a location in the data tree within the XML object. In order to accomplish this, you need to use either the appendChild() or the insertBefore() methods.
The appendChild() and insertBefore() methods do pretty much just what their names suggest. The appendChild() method adds the specified XMLNode instance to the end of the current child nodes of the object from which it is called. The appendChild() method is essential because you need it in order to create the root node within an XML object. Listing 26-7 shows an example of this.
Chapter 26 ¶ The XML and LoadVars Classes 645
Listing 26-7: Adding a Root Node with appendChild( )
var xmlBook:XML = new XML();
var xnRoot:XMLNode = new XMLNode(1, "book");
xmlBook.appendChild(xnRoot);
trace(xmlBook.toString()); // Displays: <book />
The insertBefore() method also inserts an XMLNode object as a child node of the object from which it is called. But instead of appending it to the end of the child node list, the node is inserted just before the node that is referenced as the second parameter of the method. Listing 26-8 shows an example that uses both appendChild and insertBefore() to populate an XML object.
Listing 26-8: Populating an XML Document Tree
var xmlBook:XML = new XML();
var xnRoot:XMLNode = new XMLNode(1, "book");
var xnAuthorTag:XMLNode = new XMLNode(1, "author");
var xnAuthorName:XMLNode = new XMLNode(3, "Mark Twain");
var xnTitleTag:XMLNode = new XMLNode(1, "title");
Previous << 1 .. 289 290 291 292 293 294 < 295 > 296 297 298 299 300 301 .. 419 >> Next