removeNamedItem Method
Removes an attribute from the collection.
JScript Syntax
var objXMLDOMNode = oXMLDOMNamedNodeMap.removeNamedItem(name);
Parameters
name
The string specifying the name of the attribute to remove from the collection.
Return Value
An object. Returns the node removed from the collection. Returns Null if the named node is not an attribute.
Example
Note
You can use books.xml to run this sample code.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var nodeBook;
xmlDoc.setProperty("SelectionLanguage", "XPath");
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");
WScript.Echo(nodeBook.attributes.length);
nodeBook.attributes.removeNamedItem("id");
WScript.Echo(nodeBook.attributes.length);
}
Output
1
0
C/C++ Syntax
HRESULT removeNamedItem(
BSTR name,
IXMLDOMNode **namedItem);
Parameters
name
[in]
The name of the attribute to remove from the collection.
namedItem
[out, retval]
The node removed from the collection. Returns Null if the named node is not an attribute.
Return Values
S_OK
The value returned if successful.
S_FALSE
The value when returning Null.
E_FAIL
The value returned if an error occurs.
Example
IXMLDOMNode *pIXMLDOMNode = NULL;
IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL;
BSTR bstrAttributeName = ::SysAllocString(_T("dateModified"));
IXMLDOMElement *pIXMLDOMElement = NULL;
VARIANT varValue;
HRESULT hr;
IXMLDOMDocument *pIXMLDOMDocument = NULL;
try
{
// Create an instance of DOMDocument and initialize pIXMLDOMDocument.
// Load/create an XML fragment.
hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
SUCCEEDED(hr) ? 0 : throw hr;
if(pIXMLDOMElement)
{
hr = pIXMLDOMElement->get_attributes(&pIXMLDOMNamedNodeMap);
if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap)
{
hr = pIXMLDOMNamedNodeMap->removeNamedItem(bstrAttributeName, &pIXMLDOMNode);
if(SUCCEEDED(hr) && pIXMLDOMNode)
{
pIXMLDOMNode->get_nodeValue(&varValue);
::MessageBox(NULL, _bstr_t(varValue), _T("Removed Item"), MB_OK);
pIXMLDOMNode->Release();
pIXMLDOMNode = NULL;
}
pIXMLDOMNamedNodeMap->Release();
pIXMLDOMNamedNodeMap = NULL;
}
pIXMLDOMElement->Release();
pIXMLDOMElement = NULL;
}
::SysFreeString(bstrAttributeName);
bstrAttributeName = NULL;
}
catch(...)
{
if(bstrAttributeName)
::SysFreeString(bstrAttributeName);
if(pIXMLDOMElement)
pIXMLDOMElement->Release();
if(pIXMLDOMNamedNodeMap)
pIXMLDOMNamedNodeMap->Release();
if(pIXMLDOMNode)
pIXMLDOMNode->Release();
DisplayErrorToUser();
}
// Release pIXMLDOMDocument when finished with it.
Versioning
Implemented in: MSXML 3.0 and MSXML 6.0