Adds the supplied node to the collection.
var objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem);
An object. Returns the attribute successfully added to the collection.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); var nodeBook, nodePublishDate; xmlDoc.async = false; xmlDoc.load("books.xml"); if (xmlDoc.parseError.errorCode <> 0) { var myErr = xmlDoc.parseError; alert("You have error " + myErr.reason); } else { nodePublishDate = xmlDoc.createAttribute("PublishDate"); nodePublishDate.value = String(Date()); nodeBook = xmlDoc.selectSingleNode("//book"); nodeBook.attributes.setNamedItem(nodePublishDate); alert(nodeBook.getAttribute("PublishDate")); }
Set objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem)
An object. Returns the attribute successfully added to the collection.
Dim xmlDoc As New Msxml2.DOMDocument50 Dim nodeBook As IXMLDOMElement Dim nodePublishDate As IXMLDOMAttribute 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 nodePublishDate = xmlDoc.createAttribute("PublishDate") nodePublishDate.Value = Now Set nodeBook = xmlDoc.selectSingleNode("//book") nodeBook.Attributes.setNamedItem nodePublishDate MsgBox nodeBook.getAttribute("PublishDate") End If
HRESULT setNamedItem( IXMLDOMNode *newItem, IXMLDOMNode **nameItem);
newItem
parameter is Null.HRESULT hr; IXMLDOMDocument *pIXMLDOMDocument = NULL; IXMLDOMNode *pIXMLDOMNode = NULL; IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL; BSTR bstrAttributeName = ::SysAllocString(_T("dateModified")); IXMLDOMAttribute *pIXMLDOMAttribute = NULL; IXMLDOMElement *pIXMLDOMElement = NULL; try { // Create an instance of DOMDocument and initialize pIXMLDOMDocument. // Load/create an XML fragment. hr = m_pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement); SUCCEEDED(hr) ? 0 : throw hr; if(pIXMLDOMElement) { hr = pIXMLDOMElement->get_attributes(&pIXMLDOMNamedNodeMap); if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap) { hr = m_pIXMLDOMDocument->createAttribute(bstrAttributeName, &pIXMLDOMAttribute); if(SUCCEEDED(hr) && pIXMLDOMAttribute) { hr = pIXMLDOMAttribute->put_nodeValue(_variant_t(_T("year 2000"))); hr = pIXMLDOMNamedNodeMap->setNamedItem(pIXMLDOMAttribute, &pIXMLDOMNode); if(SUCCEEDED(hr) && pIXMLDOMNode) { pIXMLDOMNode->Release(); pIXMLDOMNode = NULL; } pIXMLDOMAttribute->Release(); pIXMLDOMAttribute = 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(pIXMLDOMAttribute) pIXMLDOMAttribute->Release(); if(pIXMLDOMNode) pIXMLDOMNode->Release(); DisplayErrorToUser(); } // Release pIXMLDOMDocument when finished with it.
If an attribute already exists with the name in IXMLDOMNode
, the supplied replaces the existing attribute. The attribute name appears in its IXMLDOMNode
property.
If the newItem
node type is not NODE_ATTRIBUTE
, setNamedItem
returns an error. For example, it is not possible to modify entities or notations, which are read-only.
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.
Applies to: IXMLDOMNamedNodeMap