removeAttribute Method
Removes or replaces the named attribute.
[Script]
Script Syntax
oXMLDOMElement.removeAttribute(name);
Parameters
- name
- A string specifying the name of the attribute to be removed or replaced.
Example
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var nodeBook;
xmlDoc.setProperty("SelectionLanguage", "XPath");
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
nodeBook = xmlDoc.selectSingleNode("//book");
alert(nodeBook.attributes.length);
nodeBook.removeAttribute("id");
alert(nodeBook.attributes.length);
}
[Visual Basic]
Visual Basic Syntax
oXMLDOMElement.removeAttribute(name)
Parameters
- name
- A string specifying the name of the attribute to be removed or replaced.
Example
Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeBook As IXMLDOMElement
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.async = False
xmlDoc.Load "books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("You have error " & myErr.reason)
Else
Set nodeBook = xmlDoc.selectSingleNode("//book")
MsgBox nodeBook.Attributes.length
nodeBook.removeAttribute "id"
MsgBox nodeBook.Attributes.length
End If
[C/C++]
C/C++ Syntax
HRESULT removeAttribute(
BSTR name);
Parameters
- name [in]
- The name of the attribute to be removed or replaced.
C/C++ Return Values
- S_OK
- The value returned if successful.
- S_FALSE
- The value returned when no attribute with the given name is found.
- E_FAIL
- The value returned if an error occurs.
C/C++ Example
IXMLDOMElement *pIXMLDOMElement = NULL;
_bstr_t bstrAttributeName = _T("dateCreated");
IXMLDOMDocument *pIXMLDOMDocument = NULL;
HRESULT hr;
try
{
// Create an instance of DOMDocument and initialize pIXMLDOMDocument.
// Load/create an XML fragment.
hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pIXMLDOMElement->removeAttribute(bstrAttributeName);
if(SUCCEEDED(hr))
{
// Attribute removed.
}
pIXMLDOMElement->Release();
pIXMLDOMElement = NULL;
// Release pIXMLDOMDocument when finished with it.
}
catch(...)
{
// Release pIXMLDOMDocument if it exists.
if(pIXMLDOMElement)
pIXMLDOMElement->Release();
DisplayErrorToUser();
}
Remarks
If the specified attribute has a default value, this is equivalent to a replace operation: The current value is removed and a new attribute is created with the default value. This operation also resets the specified property of IXMLDOMNode.
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button
in the upper-left corner of the page.
See Also
Applies to: IXMLDOMElement
