setNamedItem Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

setNamedItem Method

Adds the supplied node to the collection.

[Script]

Script Syntax

var objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem);

Parameters

newItem
The object containing the attribute to be added to the collection.

Return Value

An object. Returns the attribute successfully added to the collection.

Example

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"));
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem)

Parameters

newItem
The object containing the attribute to be added to the collection.

Return Value

An object. Returns the attribute successfully added to the collection.

Example

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
[C/C++]

C/C++ Syntax

HRESULT setNamedItem(
    IXMLDOMNode *newItem,
    IXMLDOMNode **nameItem);

Parameters

newItem [in]
An attribute to be added to the collection.
nameItem [out, retval]
An attribute successfully added to the collection. If Null, no object is created.

C/C++ Return Values

S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the newItem parameter is Null.
E_FAIL
The value returned if an error occurs.

C/C++ Example

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.

Remarks

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 Language Filter in the upper-left corner of the page.

See Also

IXMLDOMNode

Applies to: IXMLDOMNamedNodeMap