JavaScript method: XML.getFirstChildElement()

This method returns an XML object representing the first child node of the current node.

Syntax

XML.getFirstChildElement();
getFirstChildElement(Element)

Arguments

The following arguments are valid for this method:

Argument Data type Description
Element String This argument contains the name of the element you want the method to use as the starting position when searching for the first child element.

Return values

An XML object or null.

The method returns an object representing the first child node of the current node or returns null if the XML object has no child node.

Example

This example displays the first child element of any element you select. The example allows you to select an element from two different input sources.

This example requires the following sample data:

  • A local XML file (for example, C:\test.xml which contains <document><parent1><child1 attribute1="a" /><child2 /></parent1><parent2><child3 /><child4 /></parent2></document>)
  • A valid XML string (for example, <document><parent1><child1 /><child2 attribute2=\"b\" /></parent1><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:\\test.xml", true );
var sourceXMLString = "<document><parent1><child1 /><child2 attribute2=\"b\" /></parent1><parent2 /></document>";
var XMLString = new XML();
XMLString = XMLString.setContent( sourceXMLString );
var XMLSource;
var TargetNode;
var searchResults;
var childResults;

/* 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( target, searchResult )
{
 if ( searchResult != null )
 {
  var elementName = searchResult.getNodeName();
  print( "Found element " + elementName + " in: \n" + searchResult + "\n" );
  return searchResult
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

/* Create a function to find the first child element of the target element */
function findFirstChild( sourceObject, targetNode )
{
 var startingNode = findTargetElement( sourceObject, targetNode )
 var firstChild = startingNode.getFirstChildElement();
 if ( firstChild != null )
 {
  return firstChild
 }
 else
 {
  return null
 }
}

/* Create a function to print the first child element of the target element */
function printChildResults( target, searchResult )
{
 if ( searchResult != null )
 {
  var childName = searchResult.getNodeName();
  print( "The first child element of " + target + " is: " + childName );
 }
 else
 {
  print( "There are no child elements of " + target );
 }
}

/* Set variable values for XMLFile and TargetNode. Run functions  */
print( "Processing XML from XMLFile... \n" );
TargetNode = "parent1"
XMLSource = XMLFile
print( "The target element we are looking for is: " + TargetNode );
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( TargetNode, searchResults );
childResults = findFirstChild( XMLSource, TargetNode );
printChildResults( TargetNode, childResults );

/* Set variable values for XMLFile and TargetNode. Run functions  */
print( "Processing XML from XMLString... \n" );
TargetNode = "parent2"
XMLSource = XMLString
print( "The target element we are looking for is: " + TargetNode );
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( TargetNode, searchResults );
childResults = findFirstChild( XMLSource, TargetNode );
printChildResults( TargetNode, childResults );