JavaScript method: XML.getAttributeNode()

This method returns an XML object containing the target attribute, or returns null if the method cannot find the target attribute in the current element. You must use the other XML get methods to navigate through an XML document.

Syntax

XML.getAttributeNode( Attribute );

Arguments

The following arguments are valid for this method:

Argument Data type Description
Attribute String This argument specifies the XML attribute you want the script to return.

Return values

An XML object or null.

The method returns an XML object containing the target attribute, or returns null if the method cannot find the target attribute in the current element.

Example

This example searches for a particular attribute of any given element in an XML document.

This example requires the following sample data:

  • A local XML file (for example, C:\test.xml which containings <document><parent1><child1 attribute1="a" /><child2 /></parent1><parent2><child3 /><child4 /></parent2></document>)
/* Create variables to store XML objects and strings.
*  Use XMLFile to store a valid XML file.
*  The script assigns values to the empty variables later on */
var XMLFile = new XML();
XMLFile = XMLFile.setContent( "C:\\test.xml", true );
var XMLSource;
var TargetNode;
var TargetAttribute;
var searchResults;

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

/* Create a function to find the attribute node of the target element */
function findAttributeNode( sourceObject, attribute )
{
 print( "Looking for attribute node " + attribute );
 var attributeNode = sourceObject.getAttributeNode( attribute );
 if ( attributeNode != null )
 {
  print( "Success. Found attribute " + attributeNode );
  return attributeNode
 }
 else
 {
  print( "Fail. Did not find attribute " + attribute );
  return null
 }
}

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