setAttributeNode Method

 

Sets or updates the supplied attribute node on this element.

JScript Syntax

var objXMLDOMAttribute = oXMLDOMElement.XMLDOMElement(DOMAttribute);  

Parameters

DOMAttribute
An object that contains the attribute node to be associated with this element.

Return Value

An object. Returns Null unless the new attribute replaces an existing attribute with the same name, in which case this method returns the previous, replaced attribute node.

Example

Note

You can use books.xml to run this sample code.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var nodeBook, nodePublishDate;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   nodePublishDate = xmlDoc.createAttribute("PublishDate");
   nodePublishDate.value = String(Date());
   nodeBook = xmlDoc.selectSingleNode("//book");
   nodeBook.setAttributeNode(nodePublishDate);
   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 setAttributeNode(  
    IXMLDOMAttribute *DOMAttribute,  
    IXMLDOMAttribute **attributeNode);  

Parameters

DOMAttribute[in]
An attribute node that is to be associated with this element.

attributeNode[out, retval]
Null unless the new attribute replaces an existing attribute with the same name, in which case this method returns the previous, replaced attribute node.

Return Values

S_OK
The value returned if successful.

E_FAIL
The value returned if an error occurs.

Example

See the example in the getAttributeNode method.

Remarks

You cannot add an existing attribute to an element until you first remove it from its previous element. Also, you cannot add a namespace-qualified attribute when it uses the same prefix as another attribute with a different namespaceURI.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

See Also

namespaceURI Property (IXMLDOMNode)
IXMLDOMElement