setAttribute Method
Sets the value of the named attribute.
oXMLDOMElement.setAttribute(name, value);
Parameters
Example
Note |
|---|
You can use books.xml to run this sample code. |
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0"); var nodeBook; xmlDoc.async = false; xmlDoc.load("books.xml"); if (xmlDoc.parseError.errorCode != 0) { var myErr = xmlDoc.parseError; WScript.Echo("You have error " + myErr.reason); } else { nodeBook = xmlDoc.selectSingleNode("//book"); nodeBook.setAttribute("PublishDate", String(Date())) WScript.Echo(nodeBook.getAttribute("PublishDate")); }
Output
Outputs the current date and time in the following format:
Thu Jun 12 14:12:38 2003
(Thursday, June 12, 2003, 2:12:38 pm)
If an attribute with the supplied name already exists, this method changes its value to the supplied value parameter. The supplied string is not parsed, so any markup, such as syntax intended to be recognized as an entity reference, is treated as literal text and must be appropriately escaped by the implementation when it is written out.
To assign an attribute value that contains entity references, the user must create IXMLDOMAttribute plus any IXMLDOMText and IXMLDOMEntityReference objects, build the appropriate subtree, and call the setAttributeNode method.
Note