JavaScript method: XML.setAttributeValue()

This method adds or updates the attribute value of the target attribute. You must use the other XML get methods to navigate through an XML document.

Syntax

XML.setAttributeValue( AttributeName, AttributeValue );

Arguments

The following arguments are valid for this method:

Argument Data type Description
AttributeName String This argument specifies the XML attribute whose value you want the script to add or update.
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

This method does not return any values.

Example

This example adds or updates the attribute value 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 contains <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 TargetValue;
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, attributeName, attributeValue )
{
 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 attributeSet = setAttributeValue( searchResult, attributeName, attributeValue );
  print( "The XML object is " + searchResult );
  return attributeSet
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

/* Create a function to set the attribute value of the target attribute */
function setAttributeValue( sourceObject, attributeName, attributeValue )
{
 print( "Setting attribute value of " + attributeName );
 var setAttributeValue = sourceObject.setAttributeValue( attributeName, attributeValue );
 var newAttributeValue = sourceObject.getAttributeValue( attributeName );
 print( "The value of " + attributeName + " is now " + newAttributeValue );
 return newAttributeValue
}

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

print( "Processing XML from XMLFile... \n" );
TargetNode = "child2";
XMLSource = XMLFile;
TargetAttribute = "attribute1";
TargetValue = "z";
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( TargetNode, searchResults, TargetAttribute, TargetValue );