JavaScript method: XML.isDocumentElement()

This method returns true if the current element is the Document Object Model (DOM) document element (the "root" node of the XML document). You must use the other XML get methods to navigate through an XML document.

Syntax

XML.isDocumentElement();

Arguments

There are no arguments for this method.

Return values

A Boolean value: true or false.

The method returns true if the current XML element is the DOM document element or false if the current XML element is not the DOM document.

Example

This example checks to see if the selected element is the DOM document element.

This example requires the following sample data:

  • A local XML file (for example, C:\test02.xml which contains <document><parent1>value1</parent1><parent2></parent2></document>)
/* Create variables to store XML objects and strings.
*  Use XMLObject to store a valid XML object such as an XML file.
*  Use sourceXMLString to store a valid XML string.
*  The script sets the content of XMLString to sourceXMLString.
*  The script assigns values to the empty variables later on */
var XMLFile = new XML();
XMLFile = XMLFile.setContent( "C:\\test02.xml", true );
var XMLSource;
var TargetNode;

/* Create a function that searches an XML object (node) for a particular
*  element (targetElem) and returns an XML object containing the element.
*  This function assumes that there is only one instance of the target
*  element in the source data. If it finds more than one element in the
*  data it only returns the first instance of the element. */
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;
}

/* Create a function to print the target element and search results */
function printFindResults( searchResult, targetNode )
{
 print( "The target element we are looking for is: " + targetNode );
 if ( searchResult != null )
 {
  var elementName = searchResult.getNodeName();
  print( "Found element " + elementName + " in: \n" + searchResult + "\n" );
  testDocElem( searchResult);
  return searchResult
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

/* Create a function to find the parent node element of the target element */
function testDocElem( sourceObject )
{
 var docElement = sourceObject.isDocumentElement();
 if ( docElement == true )
 {
  var nodeName = sourceObject.getName();
  print( "Success. The element " + nodeName + " is the DOM document element." );
  return valueFound
 }
 else
 {
  var nodeName = sourceObject.getName();
  print( "Fail. The element " + nodeName + " is not the DOM document element." );
  valueFound = null
  return valueFound
 }
}

/* Set variable values for XMLFile and TargetNode. Run functions.  */
print( "Processing XML from XMLFile... \n" );
TargetNode = "parent1"
XMLSource = XMLFile
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( searchResults, TargetNode );

/* Set variable values for XMLFile and TargetNode. Run functions.  */
print( "Processing XML from XMLFile... \n" );
TargetNode = "document"
XMLSource = XMLFile
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( searchResults, TargetNode );