Top Level

Class XMLNode

Object


public class XMLNode
extends Object

Player version: Flash Player 5

An XML document is represented in Flash by the XML class. Each element of the hierarchical document is represented by an XMLNode object.

See also
XML class



Property Summary
attributes : Object
An object containing all of the attributes of the specified XML instance.
childNodes : Array  [read-only]
An array of the specified XML object's children.
firstChild : XMLNode  [read-only]
Evaluates the specified XML object and references the first child in the parent node's child list.
lastChild : XMLNode  [read-only]
An XMLNode value that references the last child in the node's child list.
localName : String  [read-only]
The local name portion of the XML node's name.
namespaceURI : String  [read-only]
If the XML node has a prefix, namespaceURI is the value of the xmlns declaration for that prefix (the URI), which is typically called the namespace URI.
nextSibling : XMLNode  [read-only]
An XMLNode value that references the next sibling in the parent node's child list.
nodeName : String
A string representing the node name of the XML object.
nodeType : Number  [read-only]
A nodeType value, either 1 for an XML element or 3 for a text node.
nodeValue : String
The node value of the XML object.
parentNode : XMLNode  [read-only]
An XMLNode value that references the parent node of the specified XML object, or returns null if the node has no parent.
prefix : String  [read-only]
The prefix portion of the XML node name.
previousSibling : XMLNode  [read-only]
An XMLNode value that references the previous sibling in the parent node's child list.

Properties inherited from class Object
__proto__, __resolve, constructor, prototype


Constructor Summary
XMLNode(type:Number, value:String)
The XMLNode constructor lets you instantiate an XML node based on a string specifying its contents and on a number representing its node type.


Method Summary
appendChild(newChild:XMLNode) : Void
Appends the specified node to the XML object's child list.
cloneNode(deep:Boolean) : XMLNode
Constructs and returns a new XML node of the same type, name, value, and attributes as the specified XML object.
getNamespaceForPrefix(prefix:String) : String
Returns the namespace URI that is associated with the specified prefix for the node.
getPrefixForNamespace(nsURI:String) : String
Returns the prefix that is associated with the specified namespace URI for the node.
hasChildNodes() : Boolean
Specifies whether or not the XML object has child nodes.
insertBefore(newChild:XMLNode, insertPoint:XMLNode) : Void
Inserts a newChild node into the XML object's child list, before the insertPoint node.
removeNode() : Void
Removes the specified XML object from its parent.
toString() : String
Evaluates the specified XML object, constructs a textual representation of the XML structure, including the node, children, and attributes, and returns the result as a string.

Methods inherited from class Object
addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch


Property Detail

attributes Property

public attributes : Object

Player version: Flash Player 5

An object containing all of the attributes of the specified XML instance. The XML.attributes object contains one variable for each attribute of the XML instance. Because these variables are defined as part of the object, they are generally referred to as properties of the object. The value of each attribute is stored in the corresponding property as a string. For example, if you have an attribute named color, you would retrieve that attribute's value by specifying color as the property name, as the following code shows:
var myColor:String = doc.firstChild.attributes.color;

Example
The following example shows how to read and write the attributes of an XML node:
var doc:XML = new XML("<mytag name='Val'> item </mytag>");
trace(doc.firstChild.attributes.name); // Val

doc.firstChild.attributes.order = "first";
trace (doc.firstChild); // <mytag order="first" name="Val"> item </mytag>

for (attr in doc.firstChild.attributes) {
    trace (attr + " = " + doc.firstChild.attributes[attr]);
}

// order = first
// name = Val


childNodes Property

public childNodes : Array  [read-only]

Player version: Flash Player 5

An array of the specified XML object's children. Each element in the array is a reference to an XML object that represents a child node. This is a read-only property and cannot be used to manipulate child nodes. Use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

This property is undefined for text nodes (nodeType == 3).

Example
The following example shows how to use the XML.childNodes property to return an array of child nodes:
// create a new XML document
var doc:XML = new XML();

// create a root node
var rootNode:XMLNode = doc.createElement("rootNode");

// create three child nodes
var oldest:XMLNode = doc.createElement("oldest");
var middle:XMLNode = doc.createElement("middle");
var youngest:XMLNode = doc.createElement("youngest");

// add the rootNode as the root of the XML document tree
doc.appendChild(rootNode);

// add each of the child nodes as children of rootNode
rootNode.appendChild(oldest);
rootNode.appendChild(middle);
rootNode.appendChild(youngest);

// create an array and use rootNode to populate it
var firstArray:Array = doc.childNodes;
trace (firstArray); 
// output: <rootNode><oldest /><middle /><youngest /></rootNode>

// create another array and use the child nodes to populate it
var secondArray:Array = rootNode.childNodes;
trace(secondArray); 
// output: <oldest />,<middle />,<youngest />

See also
XMLNode.nodeType, XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode()

firstChild Property

public firstChild : XMLNode  [read-only]

Player version: Flash Player 5

Evaluates the specified XML object and references the first child in the parent node's child list. This property is null if the node does not have children. This property is undefined if the node is a text node. This is a read-only property and cannot be used to manipulate child nodes; use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

Example
The following example shows how to use XML.firstChild to loop through a node's child nodes:
// create a new XML document
var doc:XML = new XML();

// create a root node
var rootNode:XMLNode = doc.createElement("rootNode");

// create three child nodes
var oldest:XMLNode = doc.createElement("oldest");
var middle:XMLNode = doc.createElement("middle");
var youngest:XMLNode = doc.createElement("youngest");

// add the rootNode as the root of the XML document tree
doc.appendChild(rootNode);

// add each of the child nodes as children of rootNode
rootNode.appendChild(oldest);
rootNode.appendChild(middle);
rootNode.appendChild(youngest);

// use firstChild to iterate through the child nodes of rootNode
for (var aNode:XMLNode = rootNode.firstChild; aNode != null; aNode = aNode.nextSibling) {
    trace(aNode);
}

// output:
// <oldest />
// <middle />
// <youngest />

The following example is from the XML_languagePicker FLA file in the Examples directory and can be found in the languageXML.onLoad event handler function definition:

// loop through the strings in each language node
// adding each string as a new element in the language array
for (var stringNode:XMLNode = childNode.firstChild; stringNode != null; stringNode = stringNode.nextSibling, j++) {
    masterArray[i][j] = stringNode.firstChild.nodeValue;
}

To view the entire script, see XML_languagePicker.fla in the ActionScript samples folder:

See also
XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode()

lastChild Property

public lastChild : XMLNode  [read-only]

Player version: Flash Player 5

An XMLNode value that references the last child in the node's child list. The XML.lastChild property is null if the node does not have children. This property cannot be used to manipulate child nodes; use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

Example
The following example uses the XML.lastChild property to iterate through the child nodes of an XML node, beginning with the last item in the node's child list and ending with the first child of the node's child list:
// create a new XML document
var doc:XML = new XML();

// create a root node
var rootNode:XMLNode = doc.createElement("rootNode");

// create three child nodes
var oldest:XMLNode = doc.createElement("oldest");
var middle:XMLNode = doc.createElement("middle");
var youngest:XMLNode = doc.createElement("youngest");

// add the rootNode as the root of the XML document tree
doc.appendChild(rootNode);

// add each of the child nodes as children of rootNode
rootNode.appendChild(oldest);
rootNode.appendChild(middle);
rootNode.appendChild(youngest);

// use lastChild to iterate through the child nodes of rootNode
for (var aNode:XMLNode = rootNode.lastChild; aNode != null; aNode = aNode.previousSibling) {
    trace(aNode);
}

// output:
// <youngest />
// <middle />
// <oldest />

The following example creates a new XML packet and uses the XML.lastChild property to iterate through the child nodes of the root node:

// create a new XML document
var doc:XML = new XML("");

var rootNode:XMLNode = doc.firstChild;

// use lastChild to iterate through the child nodes of rootNode
for (var aNode:XMLNode = rootNode.lastChild; aNode != null; aNode=aNode.previousSibling) {
    trace(aNode);
}

// output:
// <youngest />
// <middle />
// <oldest />

See also
XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode(), XML

localName Property

public localName : String  [read-only]

Player version: Flash Player 8

The local name portion of the XML node's name. This is the element name without the namespace prefix. For example, the node <contact:mailbox/>bob@example.com</contact:mailbox> has the local name "mailbox", and the prefix "contact", which comprise the full element name "contact.mailbox".

You can access the namespace prefix via the prefix property of the XML node object. The nodeName property returns the full name (including the prefix and the local name).

Example
This example uses a SWF file and an XML file located in the same directory. The XML file, named "SoapSample.xml" contains the following:

      <?xml version="1.0"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">  
        <soap:Body xmlns:w="http://www.example.com/weather">
          <w:GetTemperature>
            <w:City>San Francisco</w:City>
          </w:GetTemperature>
        </soap:Body>
      </soap:Envelope>
      

The source for the SWF file contains the following script (note the comments for the Output strings):

var xmlDoc:XML = new XML()
xmlDoc.ignoreWhite = true;
xmlDoc.load("SoapSample.xml")
xmlDoc.onLoad = function(success:Boolean) 
{
    var tempNode:XMLNode = xmlDoc.childNodes[0].childNodes[0].childNodes[0];
    trace("w:GetTemperature localname: " + tempNode.localName); // Output: ... GetTemperature
    var soapEnvNode:XMLNode = xmlDoc.childNodes[0];
    trace("soap:Envelope localname: " + soapEnvNode.localName); // Output: ... Envelope
}


namespaceURI Property

public namespaceURI : String  [read-only]

Player version: Flash Player 8

If the XML node has a prefix, namespaceURI is the value of the xmlns declaration for that prefix (the URI), which is typically called the namespace URI. The xmlns declaration is in the current node or in a node higher in the XML hierarchy.

If the XML node does not have a prefix, the value of the namespaceURI property depends on whether there is a default namespace defined (as in xmlns="http://www.example.com/"). If there is a default namespace, the value of the namespaceURI property is the value of the default namespace. If there is no default namespace, the namespaceURI property for that node is an empty string ("").

You can use the getNamespaceForPrefix() method to identify the namespace associated with a specific prefix. The namespaceURI property returns the prefix associated with the node name.

Example
The following example shows how the namespaceURI property is affected by the use of prefixes. A directory contains a SWF file and an XML file. The XML file, named SoapSample.xml contains the following tags.

      <?xml version="1.0"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">  
        <soap:Body xmlns:w="http://www.example.com/weather">
          <w:GetTemperature>
            <w:City>San Francisco</w:City>
          </w:GetTemperature>
        </soap:Body>
      </soap:Envelope>
      

The source for the SWF file contains the following script (note the comments for the Output strings). For tempNode, which represents the w:GetTemperature node, the value of namespaceURI is defined in the soap:Body tag. For soapBodyNode, which represents the soap:Body node, the value of namespaceURI is determined by the definition of the soap prefix in the node above it, rather than the definition of the w prefix that the soap:Body node contains.

var xmlDoc:XML = new XML();
xmlDoc.load("SoapSample.xml");
xmlDoc.ignoreWhite = true;
xmlDoc.onLoad = function(success:Boolean) 
{
    var tempNode:XMLNode = xmlDoc.childNodes[0].childNodes[0].childNodes[0];
    trace("w:GetTemperature namespaceURI: " + tempNode.namespaceURI); 
        // Output: ... http://www.example.com/weather
        
    trace("w:GetTemperature soap namespace: " + tempNode.getNamespaceForPrefix("soap")); 
        // Output: ... http://www.w3.org/2001/12/soap-envelope
        
    var soapBodyNode:XMLNode = xmlDoc.childNodes[0].childNodes[0];
    trace("soap:Envelope namespaceURI: " + soapBodyNode.namespaceURI);
        // Output: ... http://www.w3.org/2001/12/soap-envelope
}

The following example uses XML tags without prefixes. It uses a SWF file and an XML file located in the same directory. The XML file, named NoPrefix.xml contains the following tags.


      <?xml version="1.0"?>
      <rootnode>  
        <simplenode xmlns="http://www.w3.org/2001/12/soap-envelope">
          <innernode />
        </simplenode>
      </rootnode>
      
The source for the SWF file contains the following script (note the comments for the Output strings). The rootNode does not have a default namespace, so its namespaceURI value is an empty string. The simpleNode defines a default namespace, so its namespaceURI value is the default namespace. The innerNode does not define a default namespace, but uses the default namespace defined by simpleNode, so its namespaceURI value is the same as that of simpleNode.

var xmlDoc:XML = new XML()
xmlDoc.load("NoPrefix.xml");
xmlDoc.ignoreWhite = true;
xmlDoc.onLoad = function(success:Boolean) 
{
    var rootNode:XMLNode = xmlDoc.childNodes[0];
    trace("rootNode Node namespaceURI: " + rootNode.namespaceURI); 
        // Output: [empty string]
                
    var simpleNode:XMLNode = xmlDoc.childNodes[0].childNodes[0];
    trace("simpleNode Node namespaceURI: " + simpleNode.namespaceURI);
        // Output: ... http://www.w3.org/2001/12/soap-envelope
        
    var innerNode:XMLNode = xmlDoc.childNodes[0].childNodes[0].childNodes[0];
    trace("innerNode Node namespaceURI: " + innerNode.namespaceURI);
        // Output: ... http://www.w3.org/2001/12/soap-envelope
}

See also
XMLNode.getNamespaceForPrefix(), XMLNode.getPrefixForNamespace()

nextSibling Property

public nextSibling : XMLNode  [read-only]

Player version: Flash Player 5

An XMLNode value that references the next sibling in the parent node's child list. This property is null if the node does not have a next sibling node. This property cannot be used to manipulate child nodes; use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

Example
The following example is an excerpt from the example for the XML.firstChild property, and shows how you can use the XML.nextSibling property to loop through an XML node's child nodes:
for (var aNode:XMLNode = rootNode.firstChild; aNode != null; aNode = aNode.nextSibling) {
    trace(aNode);
}

See also
XMLNode.firstChild, XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode(), XML

nodeName Property

public nodeName : String

Player version: Flash Player 5

A string representing the node name of the XML object. If the XML object is an XML element (nodeType == 1), nodeName is the name of the tag that represents the node in the XML file. For example, TITLE is the nodeName of an HTML TITLE tag. If the XML object is a text node (nodeType == 3), nodeName is null.

Example
The following example creates an element node and a text node, and checks the node name of each:
// create an XML document
var doc:XML = new XML();

// create an XML node using createElement()
var myNode:XMLNode = doc.createElement("rootNode");

// place the new node into the XML tree
doc.appendChild(myNode);

// create an XML text node using createTextNode()
var myTextNode:XMLNode = doc.createTextNode("textNode");

// place the new node into the XML tree
myNode.appendChild(myTextNode);

trace(myNode.nodeName);
trace(myTextNode.nodeName);

// output:
// rootNode
// null

The following example creates a new XML packet. If the root node has child nodes, the code loops over each child node to display the name and value of the node. Add the following ActionScript to your FLA or AS file:

var my_xml:XML = new XML("hankrudolph");
if (my_xml.firstChild.hasChildNodes()) {
    // use firstChild to iterate through the child nodes of rootNode
    for (var aNode:XMLNode = my_xml.firstChild.firstChild; aNode != null; aNode=aNode.nextSibling) {
        if (aNode.nodeType == 1) {
            trace(aNode.nodeName+":\t"+aNode.firstChild.nodeValue);
        }
    }
}

The following node names are displayed in the Output panel:The following node names write to the log file:


      output:
      username: hank
      password: rudolph
      

See also
XMLNode.nodeType

nodeType Property

public nodeType : Number  [read-only]

Player version: Flash Player 5

A nodeType value, either 1 for an XML element or 3 for a text node.

The nodeType is a numeric value from the NodeType enumeration in the W3C DOM Level 1 recommendation: www.w3.org/tr/1998/REC-DOM-Level-1-19981001/level-one-core.html. The following table lists the values:

Integer value Defined constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE

In Flash Player, the built-in XML class only supports 1 (ELEMENT_NODE) and 3 (TEXT_NODE).

Example
The following example creates an element node and a text node, and checks the node type of each:
// create an XML document
var doc:XML = new XML();

// create an XML node using createElement()
var myNode:XMLNode = doc.createElement("rootNode");

// place the new node into the XML tree
doc.appendChild(myNode);

// create an XML text node using createTextNode()
var myTextNode:XMLNode = doc.createTextNode("textNode");

// place the new node into the XML tree
myNode.appendChild(myTextNode);

trace(myNode.nodeType);
trace(myTextNode.nodeType);

// output:
// 1
// 3

See also
XMLNode.nodeValue

nodeValue Property

public nodeValue : String

Player version: Flash Player 5

The node value of the XML object. If the XML object is a text node, the nodeType is 3, and the nodeValue is the text of the node. If the XML object is an XML element (nodeType is 1), nodeValue is null and read-only

Example
The following example creates an element node and a text node, and checks the node value of each:
// create an XML document
var doc:XML = new XML();

// create an XML node using createElement()
var myNode:XMLNode = doc.createElement("rootNode");

// place the new node into the XML tree
doc.appendChild(myNode);

// create an XML text node using createTextNode()
var myTextNode:XMLNode = doc.createTextNode("textNode");

// place the new node into the XML tree
myNode.appendChild(myTextNode);

trace(myNode.nodeValue);
trace(myTextNode.nodeValue);

// output:
// null
// myTextNode

The following example creates and parses an XML packet. The code loops through each child node, and displays the node value using the firstChild property and firstChild.nodeValue. When you use firstChild to display contents of the node, it maintains the &amp; entity. However, when you explicitly use nodeValue, it converts to the ampersand character (&).

var my_xml:XML = new XML("mortongood&evil");
trace("using firstChild:");
for (var i = 0; i<my_xml.firstChild.childNodes.length; i++) {
    trace("\t"+my_xml.firstChild.childNodes[i].firstChild);
}
trace("");
trace("using firstChild.nodeValue:");
for (var i = 0; i<my_xml.firstChild.childNodes.length; i++) {
    trace("\t"+my_xml.firstChild.childNodes[i].firstChild.nodeValue);
}

The following information is displayed in the Output panel:The following information writes to the log file:


      using firstChild:
         morton
         good&evil
      
      using firstChild.nodeValue:
         morton
         good&evil
      

See also
XMLNode.nodeType

parentNode Property

public parentNode : XMLNode  [read-only]

Player version: Flash Player 5

An XMLNode value that references the parent node of the specified XML object, or returns null if the node has no parent. This is a read-only property and cannot be used to manipulate child nodes; use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

Example
The following example creates an XML packet and displays the parent node of the username node in the Output panel:The following example creates an XML packet and writes the parent node of the username node to the log file:
var my_xml:XML = new XML("mortongood&evil");

// first child is the <login /> node
var rootNode:XMLNode = my_xml.firstChild;

// first child of the root is the <username /> node
var targetNode:XMLNode = rootNode.firstChild; 
trace("the parent node of '"+targetNode.nodeName+"' is: "+targetNode.parentNode.nodeName);
trace("contents of the parent node are:\n"+targetNode.parentNode);
Output (line breaks added for clarity):

      the parent node of 'username' is: login
      contents of the parent node are:
      
         morton
         good&evil
      
      

See also
XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode(), XML

prefix Property

public prefix : String  [read-only]

Player version: Flash Player 8

The prefix portion of the XML node name. For example, the node <contact:mailbox/>bob@example.com</contact:mailbox> prefix "contact" and the local name "mailbox", which comprise the full element name "contact.mailbox".

The nodeName property of an XML node object returns the full name (including the prefix and the local name). You can access the local name portion of the element's name via the localName property.

Example
A directory contains a SWF file and an XML file. The XML file, named "SoapSample.xml" contains the following:

      <?xml version="1.0"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">  
        <soap:Body xmlns:w="http://www.example.com/weather">
          <w:GetTemperature>
            <w:City>San Francisco</w:City>
          </w:GetTemperature>
        </soap:Body>
      </soap:Envelope>
      

The source for the SWF file contains the following script (note the comments for the Output strings):

var xmlDoc:XML = new XML();
xmlDoc.ignoreWhite = true;
xmlDoc.load("SoapSample.xml");
xmlDoc.onLoad = function(success:Boolean) 
{
    var tempNode:XMLNode = xmlDoc.childNodes[0].childNodes[0].childNodes[0];
    trace("w:GetTemperature prefix: " + tempNode.prefix); // Output: ... w
    var soapEnvNode:XMLNode = xmlDoc.childNodes[0];
    trace("soap:Envelope prefix: " + soapEnvNode.prefix); // Output: ... soap
}


previousSibling Property

public previousSibling : XMLNode  [read-only]

Player version: Flash Player 5

An XMLNode value that references the previous sibling in the parent node's child list. The property has a value of null if the node does not have a previous sibling node. This property cannot be used to manipulate child nodes; use the appendChild(), insertBefore(), and removeNode() methods to manipulate child nodes.

Example
The following example is an excerpt from the example for the XML.lastChild property, and shows how you can use the XML.previousSibling property to loop through an XML node's child nodes:
for (var aNode:XMLNode = rootNode.lastChild; aNode != null; aNode = aNode.previousSibling) {
    trace(aNode);
}

See also
XMLNode.lastChild, XMLNode.appendChild(), XMLNode.insertBefore(), XMLNode.removeNode(), XML


Constructor Detail

XMLNode Constructor

public XMLNode(type:Number, value:String)

Player version: Flash Player 8

The XMLNode constructor lets you instantiate an XML node based on a string specifying its contents and on a number representing its node type.

Parameters
type:Number — An integer representing the node type:

Integer value Defined constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 NTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE

In Flash Player, the XML class only supports node types 1 (ELEMENT_NODE) and 3 (TEXT_NODE).

value:String — For a text node, this is the text of the node; for an element node, this is the contents of the tag.

Example
var ELEMENT_NODE:Number = 1;
var node1:XMLNode = new XMLNode(ELEMENT_NODE, "fullName");
    
var TEXT_NODE:Number = 3;
var node2:XMLNode = new XMLNode(TEXT_NODE, "Justin Case");

// Create a new XML document
var doc:XML = new XML();
        
// Create a root node
var rootNode:XMLNode = doc.createElement("root");
        
// Add the rootNode as the root of the XML document tree
doc.appendChild(rootNode);
        
// Build the rest of the document:
rootNode.appendChild(node1);
node1.appendChild(node2);

trace(doc);

// Output: Justin Case



Method Detail

appendChild Method

public appendChild(newChild:XMLNode) : Void

Player version: Flash Player 5

Appends the specified node to the XML object's child list. This method operates directly on the node referenced by the childNode parameter; it does not append a copy of the node. If the node to be appended already exists in another tree structure, appending the node to the new location will remove it from its current location. If the childNode parameter refers to a node that already exists in another XML tree structure, the appended child node is placed in the new tree structure after it is removed from its existing parent node.

Parameters
newChild:XMLNode — An XMLNode that represents the node to be moved from its current location to the child list of the my_xml object.

Example
This example does the following things in the order shown:

var doc1:XML = new XML();
var doc2:XML = new XML();

// create a root node and add it to doc1
var rootnode:XMLNode = doc1.createElement("root");
doc1.appendChild(rootnode);
trace ("doc1: " + doc1); // output: doc1: <root />
trace ("doc2: " + doc2); // output: doc2:

// move the root node to doc2
doc2.appendChild(rootnode);
trace ("doc1: " + doc1); // output: doc1:
trace ("doc2: " + doc2); // output: doc2: <root />

// clone the root node and append it to doc1
var clone:XMLNode = doc2.firstChild.cloneNode(true);
doc1.appendChild(clone);
trace ("doc1: " + doc1); // output: doc1: <root />
trace ("doc2: " + doc2); // output: doc2: <root />

// create a new node to append to root node (named clone) of doc1
var newNode:XMLNode = doc1.createElement("newbie");
clone.appendChild(newNode);
trace ("doc1: " + doc1); // output: doc1: <root><newbie /></root>


cloneNode Method

public cloneNode(deep:Boolean) : XMLNode

Player version: Flash Player 5

Constructs and returns a new XML node of the same type, name, value, and attributes as the specified XML object. If deep is set to true, all child nodes are recursively cloned, resulting in an exact copy of the original object's document tree.

The clone of the node that is returned is no longer associated with the tree of the cloned item. Consequently, nextSibling, parentNode, and previousSibling all have a value of null. If the deep parameter is set to false, or the my_xml node has no child nodes, firstChild and lastChild are also null.

Parameters
deep:Boolean — A Boolean value; if set to true, the children of the specified XML object will be recursively cloned.

Returns
XMLNode — An XMLNode Object.

Example
The following example shows how to use the XML.cloneNode() method to create a copy of a node:
// create a new XML document
var doc:XML = new XML();

// create a root node
var rootNode:XMLNode = doc.createElement("rootNode");

// create three child nodes
var oldest:XMLNode = doc.createElement("oldest");
var middle:XMLNode = doc.createElement("middle");
var youngest:XMLNode = doc.createElement("youngest");

// add the rootNode as the root of the XML document tree
doc.appendChild(rootNode);

// add each of the child nodes as children of rootNode
rootNode.appendChild(oldest);
rootNode.appendChild(middle);
rootNode.appendChild(youngest);

// create a copy of the middle node using cloneNode()
var middle2:XMLNode = middle.cloneNode(false);

// insert the clone node into rootNode between the middle and youngest nodes
rootNode.insertBefore(middle2, youngest);
trace(rootNode);
// output (with line breaks added): 
// <rootNode>
// <oldest />
// <middle />
// <middle />
// <youngest />
// </rootNode>

// create a copy of rootNode using cloneNode() to demonstrate a deep copy
var rootClone:XMLNode = rootNode.cloneNode(true);

// insert the clone, which contains all child nodes, to rootNode
rootNode.appendChild(rootClone);
trace(rootNode); 
// output (with line breaks added): 
// <rootNode>
// <oldest />
// <middle />
// <middle />
// <youngest />
// <rootNode>
// <oldest />
// <middle />
// <middle />
// <youngest />
// </rootNode>
// </rootNode>


getNamespaceForPrefix Method

public getNamespaceForPrefix(prefix:String) : String

Player version: Flash Player 8

Returns the namespace URI that is associated with the specified prefix for the node. To determine the URI, getPrefixForNamespace() searches up the XML hierarchy from the node, as necessary, and returns the namespace URI of the first xmlns declaration for the given prefix.

If no namespace is defined for the specified prefix, the method returns null.

If you specify an empty string ("") as the prefix and there is a default namespace defined for the node (as in xmlns="http://www.example.com/"), the method returns that default namespace URI.

Parameters
prefix:String — The prefix for which the method returns the associated namespace.

Returns
String — The namespace that is associated with the specified prefix.

Example
The following example creates a very simple XML object and outputs the result of a call to getNamespaceForPrefix()
function createXML():XMLNode {
    var str:String = "<Outer xmlns:exu=\"http://www.example.com/util\">"
        + "<exu:Child id='1' />"
        + "<exu:Child id='2' />"
        + "<exu:Child id='3' />"
        + "</Outer>";
    return new XML(str).firstChild;
}

var xml:XMLNode = createXML();
trace(xml.getNamespaceForPrefix("exu")); // output: http://www.example.com/util
trace(xml.getNamespaceForPrefix(""));    // output: null

See also
XMLNode.getPrefixForNamespace(), XMLNode.namespaceURI

getPrefixForNamespace Method

public getPrefixForNamespace(nsURI:String) : String

Player version: Flash Player 8

Returns the prefix that is associated with the specified namespace URI for the node. To determine the prefix, getPrefixForNamespace() searches up the XML hierarchy from the node, as necessary, and returns the prefix of the first xmlns declaration with a namespace URI that matches nsURI.

If there is no xmlns assignment for the given URI, the method returns null. If there is an xmlns assignment for the given URI but no prefix is associated with the assignment, the method returns an empty string ("").

Parameters
nsURI:String — The namespace URI for which the method returns the associated prefix.

Returns
String — The prefix associated with the specified namespace.

Example
The following example creates a very simple XML object and outputs the result of a call to the getPrefixForNamespace() method. The Outer XML node, which is represented by the xmlDoc variable, defines a namespace URI and assigns it to the exu prefix. Calling the getPrefixForNamespace() method with the defined namespace URI ("http://www.example.com/util") returns the prefix exu, but calling this method with an undefined URI ("http://www.example.com/other") returns null. The first exu:Child node, which is represented by the child1 variable, also defines a namespace URI ("http://www.example.com/child"), but does not assign it to a prefix. Calling this method on the defined, but unassigned, namespace URI returns an empty string.
function createXML():XMLNode {
    var str:String = "<Outer xmlns:exu=\"http://www.example.com/util\">"
        + "<exu:Child id='1' xmlns=\"http://www.example.com/child\"/>"
        + "<exu:Child id='2' />"
        + "<exu:Child id='3' />"
        + "</Outer>";
    return new XML(str).firstChild;
}

var xmlDoc:XMLNode = createXML();
trace(xmlDoc.getPrefixForNamespace("http://www.example.com/util")); // output: exu
trace(xmlDoc.getPrefixForNamespace("http://www.example.com/other")); // output: null

var child1:XMLNode = xmlDoc.firstChild;
trace(child1.getPrefixForNamespace("http://www.example.com/child")); // output: [empty string]
trace(child1.getPrefixForNamespace("http://www.example.com/other")); // output: null

See also
XMLNode.getNamespaceForPrefix(), XMLNode.namespaceURI

hasChildNodes Method

public hasChildNodes() : Boolean

Player version: Flash Player 5

Specifies whether or not the XML object has child nodes.

Returns
Booleantrue if the specified XMLNode has one or more child nodes; otherwise false.

Example
The following example creates a new XML packet. If the root node has child nodes, the code loops over each child node to display the name and value of the node. Add the following ActionScript to your FLA or AS file:
var my_xml:XML = new XML("hankrudolph");
if (my_xml.firstChild.hasChildNodes()) {
// use firstChild to iterate through the child nodes of rootNode
    for (var aNode:XMLNode = my_xml.firstChild.firstChild; aNode != null; aNode=aNode.nextSibling) {
        if (aNode.nodeType == 1) {
            trace(aNode.nodeName+":\t"+aNode.firstChild.nodeValue);
        }
    }
}

The following is displayed in the Output panel:

The following is written to the log file:


      output:
      username: hank
      password: rudolph 
      


insertBefore Method

public insertBefore(newChild:XMLNode, insertPoint:XMLNode) : Void

Player version: Flash Player 5

Inserts a newChild node into the XML object's child list, before the insertPoint node. If insertPoint is not a child of the XMLNode object, the insertion fails.

Parameters
newChild:XMLNode — The XMLNode object to be inserted.
insertPoint:XMLNode — The XMLNode object that will follow the newChild node after the method is invoked.

Example
The following inserts a new XML node between two existing nodes:
var my_xml:XML = new XML("<a>1</a>\n<c>3</c>");
var insertPoint:XMLNode = my_xml.lastChild;
var newNode:XML = new XML("<b>2</b>\n");
my_xml.insertBefore(newNode, insertPoint);
trace(my_xml); 

See also
XML, XMLNode.cloneNode()

removeNode Method

public removeNode() : Void

Player version: Flash Player 5

Removes the specified XML object from its parent. Also deletes all descendants of the node.

Example
The following example creates an XML packet, and then deletes the specified XML object and its descendant nodes:
var xml_str:String = "<state name=\"California\"><city>San Francisco</city></state>";

var my_xml:XML = new XML(xml_str);
var cityNode:XMLNode = my_xml.firstChild.firstChild;
trace("before XML.removeNode():\n"+my_xml);
cityNode.removeNode();
trace("");
trace("after XML.removeNode():\n"+my_xml);

// output (line breaks added for clarity):
// 
// before XML.removeNode():
// <state name="California">
// <city>San Francisco</city>
// </state>
// 
// after XML.removeNode():
// <state name="California" />


toString Method

public toString() : String

Player version: Flash Player 5

Evaluates the specified XML object, constructs a textual representation of the XML structure, including the node, children, and attributes, and returns the result as a string.

For top-level XML objects (those created with the constructor), the XML.toString() method outputs the document's XML declaration (stored in the XML.xmlDecl property), followed by the document's DOCTYPE declaration (stored in the XML.docTypeDecl property), followed by the text representation of all XML nodes in the object. The XML declaration is not output if the XML.xmlDecl property is undefined. The DOCTYPE declaration is not output if the XML.docTypeDecl property is undefined.

Returns
String — String.

Example
The following code uses the toString() method to convert an XMLNode object to a String, and then uses the toUpperCase() method of the String class:
var xString = "<first>Mary</first>"
        + "<last>Ng</last>"
var my_xml:XML = new XML(xString);
var my_node:XMLNode = my_xml.childNodes[1];
trace(my_node.toString().toUpperCase());
        // output: <LAST>NG</LAST>

See also
XML.docTypeDecl, XML.xmlDecl