JavaScript method: XML.createNode()

This method creates an XML node from an XML object. It requires an existing XML object with a document element, but it does not update the existing XML object. The new method requires a rootname.

Syntax

XML.createNode( type, name, value );

Arguments

The following arguments are valid for this method:

Argument Data type Description
type Integer This argument specifies the XML DOM node type for the node you want the script to create. This method only accepts XML DOM types 1, 2, and 3. See the W3C Web site for a complete listing of XML DOM node-types by integer.
name String This argument specifies the text string you want the script to add as the XML node name. The string argument must contain characters valid for XML (for example, the string cannot include the characters < or > except as XML entities such as &lt; and &gt;).
value String This argument specifies the text string you want the script to add as the XML node value. The string argument must contain characters valid for XML (for example, the string cannot include the characters < or > except as XML entities such as &lt; and &gt;).

Return values

An XML object or null.

The method returns an XML object containing the new node or returns null if the method cannot create the node.

Example

This example creates an XML node from an XML source object.

This example requires the following sample data:

  • An XML object (for example, an object containing "<document> </document>")
var XMLObject = new XML("document");
var NewNodeName;
var NewNodeValue;
var NewNodeType;
XMLObject = XMLObject.setContent( "<document> </document>" );

function createNewNode( type, name, value, xmlSource )
{
 print( "The XML source object is: " + xmlSource);
 print( "Creating node of type: " + type + " named: " + name + " with value: " + value );
 var NewNode = xmlSource.createNode( type, name, value );
 print( "The XML source object remains unchanged: " + XMLObject );
 print( "The new node converted to an XML string is: " + NewNode.toXMLString() );
 print( "Applying the getName method to the new node produces: " + NewNode.getName() );
 print( "Applying the getValue method on the new node produces: " + NewNode.getValue() );
}

NewNodeType = 1;
NewNodeName = "node";
NewNodeValue = "test";
createNewNode( NewNodeType, NewNodeName, NewNodeValue, XMLObject );

NewNodeType = 2;
NewNodeName = "id";
NewNodeValue = "test";
createNewNode( NewNodeType, NewNodeName, NewNodeValue, XMLObject );

NewNodeType = 3;
NewNodeName = null;
NewNodeValue = "Text node";
createNewNode( NewNodeType, NewNodeName, NewNodeValue, XMLObject );