JavaScript method: XML.appendNode()

This method inserts an XML node into an XML object. There must be an existing XML object with a document element. This method does not update the existing XML object.

Syntax

XML.appendNode( name );

Arguments

The following arguments are valid for this method:

Argument Data type Description
name String This argument specifies the XML node name you want to append to the target XML object. 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 append the node to the current target node.

Example

This example appends an XML node to 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;
var NewNodeName;
var NewNodeValue;
var NewNodeType;
var TargetNode;
XMLObject = XMLObject.setContent( "<document> </document>" );

function findTargetElement( node, targetElem )
{
 var topNodeName = node.getNodeName();
 while (node != null && node.getNodeName() != targetElem )
 {
  var childNode = node.getFirstChildElement();
  if (childNode == null)
  {
   childNode = node.getNextSiblingElement();
   while (childNode == null)
   {
    node = node.getParentNode();
    if ( node == null || topNodeName == node.getNodeName() )
    {
     return null;
    }
    childNode = node.getNextSiblingElement();
   }
   node = childNode;
  }
  else
  {
   node = childNode;
  }
 }
 return node;
}

function appendNewNode( type, name, value, xmlSource, target )
{
 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( "Appending " + NewNode.toXMLString() + " to: " + target );
 var FoundNode = findTargetElement( xmlSource, target );
 var appendedNode = FoundNode.appendNode( NewNode );
 print( "The new XML object is: " +  xmlSource );
}

NewNodeType = 1;
NewNodeName = "node";
NewNodeValue = "test";
TargetNode = "document";
appendNewNode( NewNodeType, NewNodeName, NewNodeValue, XMLObject, TargetNode );