JavaScript method: XML.setValue()

This method adds or updates the value of the current element. You must use the other XML get methods to navigate through an XML document. This method is a synonym for setNodeValue();.

Syntax

XML.setValue( String );

Arguments

The following arguments are valid for this method:

Argument Data type Description
String String This argument specifies the text string you want the script to add as the XML element value. The string argument must contain characters valid for XML (for example, the string cannot include the characters < or > except as XML entities such as &lt; and &gt;).

Return values

An XML object or null.

The method returns an XML object containing the new value of the current XML element or returns null if the method cannot set a value for the element.

Example

This example sets the value of the elements in an XML document.

This example requires the following sample data:

  • A local XML file (for example, C:\test03.xml which contains <document><parent1></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:\\test03.xml", true );
var TargetNode;
var TargetValue;
var findElement;

/* 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 setElementValue( searchResult, targetNode, newValue )
{
 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" );
  searchResult.setValue( newValue );
  print( "The new value of " + targetNode + " is: " + searchResult );
  return searchResult
 }
 else
 {
  print( "Cannot find " + targetNode );
  return null
 }
}

TargetNode = "parent1";
TargetValue = "1234";
findElement = findTargetElement( XMLFile, TargetNode );
setElementValue( findElement, TargetNode, TargetValue );