Developer Resources

XmlNode

Overview

The XmlNode class allows XML data to be loaded and saved, as well as accessed as a tree data structure of XmlNodes.

Constructor

XmlNode()

Methods

XmlNode.appendAttribute
Adds an attribute to the current XmlNode.
XmlNode.appendChild
Appends an XmlNode child to the current XmlNode.
XmlNode.cloneNode
Clones the current XmlNode.
XmlNode.getAttributes
Gets an array of the node's attributes
XmlNode.getChild
Gets an XmlNode child of the current XmlNode.
XmlNode.getChildCount
Gets the number of XmlNode children for the current XmlNode.
XmlNode.getChildNodes
Returns an array of node children
XmlNode.getNodeName
Gets the name for the current XmlNode.
XmlNode.getNodeValue
Gets the contents for the current XmlNode.
XmlNode.hasAttributes
Determines if the node has any attributes
XmlNode.hasChildNodes
Determines if the node has any child nodes
XmlNode.isEmpty
Indicates whether the current XmlNode is empty or not.
XmlNode.load
Loads an XML file into an XML data structure.
XmlNode.parse
Parses the input into an XML data structure.
XmlNode.removeChild
Deletes an XmlNode child of the current XmlNode.
XmlNode.save
Saves an XML data structure to a file.
XmlNode.saveToString
Saves an XML data structure to a string
XmlNode.setNodeName
Sets the name for the current XmlNode.
XmlNode.setNodeValue
Sets the value of the current XmlNode.

Example

// a simple parsing example
var test ="


   Contents
   
   

";

var node = new XmlNode;
node.parse(test);

alert("Root tag name is: " + node.getNodeName());

var child_nodes = node.getChildNodes();
for (var i in child_nodes)
{
    alert("child " + i +
               " has a tag name of: " +
               child_nodes[i].getNodeName());
               
    if (child_nodes[i].hasAttributes())
    {
        var attrs = child_nodes[i].getAttributes();
        for (var a in attrs)
        {
            alert("child with node name " +
                       child_nodes[i].getNodeName() +
                       " has an attribute " + a +
                       " with a value of " + attrs[a]);
        }
    }              
}

XmlNode.appendAttribute

function XmlNode.appendAttribute(name : String, value : String) : Boolean

Arguments

name
The name of the attribute to add.
value
The value of the attribute to add. Results: Returns true if the attribute and value are added to the XmlNode, and false otherwise.

Description

Adds an attribute to the current XmlNode, with the specified name and value. If the attribute name and value are not-empty and they are properly set, the function returns true. However, if the name or value are empty, or the name or value are not properly set, the function returns false.

XmlNode.appendChild

function XmlNode.appendChild(node_name : String, node_content : String) : XmlNode

Arguments

node
The XmlNode to append
node_name
The name of the XmlNode being created
node_value
The value of the XmlNode being created

Returns

A reference to the appended node

Description

Appends an XmlNode to the current XmlNode, using either the specified XmlNode, or creating a new one with the specified name node_name and node_content. If no parameters are specified, an empty child is created and appended. If node_name is specified but node_value isn't specified, a child having node_name is created and appended, but without any content.

XmlNode.cloneNode

function XmlNode.cloneNode(deep : Boolean) : XmlNode

Arguments

deep
A flag indicating whether or not to clone this node's children when cloning this node

Returns

A reference to the cloned node

Description

Clones this XmlNode and returns a reference to the new node. If deep is set to true, this node's children will also be cloned; otherwise, only the name, value and properties of this node will be cloned. By deep isn't specified, then by default the children will be cloned.

XmlNode.getAttributes

function XmlNode.getAttributes() : Object

Returns

Returns an object with the names and values of the node's attributes

Description

Returns an object with named elements corresponding to the attribute names of the node. Each of these elements, in turn, contain the string value associated with each attribute

XmlNode.getChild

function XmlNode.getChild(index : Integer) : XmlNode
function XmlNode.getChild(name : String) : XmlNode

Arguments

index
The index of the XmlNode child to return.
name
The name of the XmlNode child to return.

Returns

The XmlNode child corresponding to the input index or name, or null if no corresponding child exists.

Description

Gets an XmlNode child of the current XmlNode from a specified index or name. The function returns the given XmlNode corresponding to the index or the name. However, the function returns null if the index is not in the valid range of indexes for the child XmlNodes, or the name is not a valid name of a child.

XmlNode.getChildCount

function XmlNode.getChildCount() : Integer

Returns

The number of XmlNode children for the current XmlNode.

Description

Returns the number of immediate XmlNode children for the current XmlNode.

XmlNode.getChildNodes

function XmlNode.getChildNodes() : Array(XmlNode)

Returns

An array of XmlNode objects representing all node children

Description

Calling getChildNodes() returns an array of XmlNode objects which represent all children of the node.

XmlNode.getNodeName

function XmlNode.getNodeName() : String

Returns

Returns the name for the current XmlNode.

XmlNode.getNodeValue

function XmlNode.getNodeValue() : String

Returns

Returns the value of the current XmlNode.

Description

Returns the value of the current XmlNode.

XmlNode.hasAttributes

function XmlNode.hasAttributes() : Boolean

Returns

True if the node has any attributes, false otherwise

XmlNode.hasChildNodes

function XmlNode.hasChildNodes() : Boolean

Returns

True if the node has any child nodes, false otherwise

XmlNode.isEmpty

function XmlNode.isEmpty() : Boolean

Returns

Returns true if the current XmlNode is empty, and false otherwise.

Description

Indicates whether the current XmlNode is empty or not. The function returns true if the current XmlNode is empty, and false otherwise.

XmlNode.load

function XmlNode.load(input : String) : Boolean

Arguments

input
The file to load into an XML data structure.

Returns

Returns true if the input file is successfully loaded, and false otherwise.

Description

Loads an XML file, specfied by input, into an XML data structure. If the input file is successfully loaded, the function returns true. If the input file cannot be loaded, the function returns false.

XmlNode.parse

function XmlNode.parse(text : String) : Boolean

Arguments

text
The text to parse.

Returns

Returns true if the text is successfully parsed into an XML data structure, and false otherwise.

Description

Parses the input text to create an XML data structure. If text is parsed successfully, the function returns true. If text cannot be parsed, the function returns false.

XmlNode.removeChild

function XmlNode.removeChild(index : Integer) : Boolean
function XmlNode.removeChild(node : XmlNode) : Boolean

Arguments

index
The index of the XmlNode child to delete.

Returns

Returns true if the XmlNode child is deleted, and false otherwise.

Description

Deletes an XmlNode child of the current XmlNode at the specified index. If the child node is successfully deleted, the function returns true. If the child not is not deleted, the function returns false.

XmlNode.save

function XmlNode.save(output : String) : Boolean

Arguments

output
The file to which to save the XML data.

Returns

Returns true if the XML data is successfully saved to the output file, and false otherwise.

Description

Saves an XML data structure to a file, specified by output. If the XML data is successfully saved to the output, file, the function returns true. If the XML data is not successfully saved to the output file, the function returns false.

XmlNode.saveToString

function XmlNode.saveToString() : String

Returns

Returns a string representation of an XML data structure.

Description

Saves an XML data structure to a string.

XmlNode.setNodeName

function XmlNode.setNodeName(name : String)

Arguments

name
The new name for the XmlNode child.

Description

Sets the name for the current XmlNode from the specified name.

XmlNode.setNodeValue

function XmlNode.setNodeValue(new_value : String)

Arguments

new_value
The new value for the current XmlNode.

Description

Sets the value of the current XmlNode.