Share via


setAttribute Method

 

Sets the value of the named attribute.

JScript Syntax

oXMLDOMElement.setAttribute(name, value);  

Parameters

name
The string specifying the name of the attribute. If the attribute with that name already exists, its value is changed. If the attribute with that name does not exist, it is created.

value
The variant that supplies the value for the named attribute.

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)

C/C++ Syntax

HRESULT setAttribute(  
    BSTR name,  
    VARIANT value);  

Parameters

name[in]
The name of the attribute. If an attribute with that name already exists, its value is changed. If an attribute with that name does not exist, it is created.

value[in]
The value for the named attribute.

Return Values

S_OK
The value returned if successful.

E_FAIL
The value returned if an error occurs.

Example

See the example in the getAttribute method.

Remarks

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.

Versioning

MSXML 3.0 and MSXML 6.0

See Also

IXMLDOMAttribute
IXMLDOMText
IXMLDOMEntityReference
setAttributeNode Method
IXMLDOMElement