JavaScript method: XML.addAttribute()

This method inserts an XML attribute and attribute value into the current element. It requires you use the other XML get methods to navigate through an XML document.

Syntax

XML.addAttribute( AttributeName, AttributeValue );

Arguments

The following arguments are valid for this method:

Argument Data type Description
AttributeName String This argument specifies the text string you want the script to use as the XML attribute name. The string argument must contain characters valid for an XML element (for example, the string cannot include the characters < or >).
AttributeValue String This argument specifies the text string you want the script to use as the XML attribute value. This argument must contain characters valid for an XML element (for example, the string cannot include the characters < or >).

Return values

An XML object or null.

If successful, the method returns the updated XML element. Otherwise, it returns null.

Example

This example adds a new attribute and attribute value to any element you select from an XML document.

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;
var searchResults;
var printResults;
var AttributeName;
var AttributeValue;

/* 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 createNewAttribute( searchResult, targetNode, attrName, attrValue )
{
 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" );
  print( "Adding new attribute " + attrName + " to element " + elementName );
  searchResult.addAttribute( attrName, attrValue );
  print( "The new object is:\n" + searchResult );
  return searchResult
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

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