JavaScript method: XML.getDocumentElement()

This method searches an XML object for the DOM document element (or "root" node). It creates an XML object containing the document element and all child elements from an XML object or XML string.

Note You cannot use this method on objects that you have converted using the toXMLString method.

Syntax

XML.getDocumentElement();

Arguments

There are no arguments for this method.

Return values

An XML object or null.

If successful, this method returns an XML object containing the document element and all of its child elements. This method returns null if applied to an empty or invalid XML object.

Example

This example displays the XML document element from four different input sources.

This example requires the following sample data:

  • A valid XML object (for example, the list of currently logged on users stored in system.users)
  • A valid XML string (for example, <document><parent1><child1 attribute1=\"1\" /></parent1><parent2 /></document>)
  • A noncompliant XML string (for example, <document1><parent1 /></document1><document2><parent2 /></document2>)
  • An empty XML object
/* Create variables to store XML objects and strings.
*  Use goodXML to store a valid XML string.
*  Use badXML to store any erroneous XML string, in this case a second document element.
*  Use xmlObject to store a valid XML object such as the system.users list
*  The script sets the content of xmlString to goodXML.
*  The script sets the content of xmlBad to badXML.
*  The script sets the content of xmlEmpty to an XML object. */
var goodXML = "<document><parent1><child1 attribute1=\"1\" /></parent1><parent2 /></document>"
var badXML = "<document1><parent1 /></document1><document2><parent2 /></document2>"
var xmlObject = system.users
var xmlString = new XML();
xmlString = xmlString.setContent( goodXML );
var xmlBad = new XML();
xmlBad = xmlBad.setContent( badXML );
print( "The value of xmlBad is:\n" + xmlBad );
var xmlEmpty = new XML();

/* Create a function to execute the getDocumentElement method on the document argument. */
function getDocElem( document )
{
 var DocElem = document.getDocumentElement();
 if ( DocElem != null )
 {
  var DocElemName = DocElem.getNodeName();
  print( "Success. Found the document element " + DocElemName + " in the following object: \n" + document );
  return DocElem
 }
 else
 {
  print( "Error. Did not find document element in the following object: \n" + document );
  return null
 }
}

/* Run the getDocElem and getDocElemName functions on four input sources.
*  Print the objects returned by each function. */
print( "Now searching for a document element in a valid XML object...\n" );
getDocElem( xmlObject );
print( "Now searching for a document element in a valid XML string...\n" );
getDocElem( xmlString );
print( "Now searching for a document element in a non-compliant XML string...\n" );
getDocElem( xmlBad );
print( "Now searching for a document element in an empty XML object...\n" );
getDocElem( xmlEmpty );