IXMLDOMNamedNodeMap

MSXML 5.0 SDK

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

IXMLDOMNamedNodeMap

Adds support for namespaces and iteration through the collection of attribute nodes.

[Script]

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var nodeBook, namedNodeMap;
xmlDoc.async = false;
xmlDoc.setProperty("SelectionLanguage", "XPath");
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   nodeBook = xmlDoc.selectSingleNode("//book");
   namedNodeMap = nodeBook.attributes;
   alert(namedNodeMap.length);
}
[Visual Basic]

Example

Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeBook As IXMLDOMNode
Dim namedNodeMap As IXMLDOMNamedNodeMap
xmlDoc.async = False
xmlDoc.setProperty "SelectionLanguage", "XPath"
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")
   Set namedNodeMap = nodeBook.Attributes
   MsgBox namedNodeMap.length
End If
[C/C++]

Example

The following C/C++ example creates and appends a new attribute to the root document element.

#import "msxml5.dll"
using namespace MSXML2;

inline void TESTHR( HRESULT _hr ) 
   { if FAILED(_hr) throw(_hr); }

void XMLDOMNamedNodeMap()
{
   try {
      IXMLDOMDocumentPtr docPtr;
      IXMLDOMNodePtr DOMNodePtr;
      IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;

      //init
      TESTHR(CoInitialize(NULL)); 
      TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.5.0"));
      
      VARIANT vtTemp;

      vtTemp.vt=VT_I2;
      vtTemp.iVal = MSXML2::NODE_ATTRIBUTE;

      // load a document
      _variant_t varXml("C:\\book.xml");
      _variant_t varOut((bool)TRUE);
      varOut = docPtr->load(varXml);
      if ((bool)varOut == FALSE)
         throw(0);
      
      DOMNodePtr = docPtr->createNode(vtTemp, "Sci-Fi", "");
      DOMNamedNodeMapPtr = docPtr->documentElement->attributes;
      DOMNamedNodeMapPtr->setNamedItem(DOMNodePtr);

      MessageBox(NULL, _bstr_t(docPtr->xml), _T("New Document"), MB_OK);

   } catch(...)
   {
      MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
   }
   CoUninitialize();
}

Remarks

IXMLDOMNamedNodeMap is a node collection that allows access by name as well as by index. This collection is typically used for attributes.

The World Wide Web Consortium (W3C) Document Object Model (DOM) definition does not require NamedNodeMap objects to be maintained in any particular order. Objects contained in a NamedNodeMap can also be accessed by an ordinal index, but this is simply to allow convenient enumeration and does not imply that the DOM specifies an order for these nodes. DOM implementations are not required to preserve the node order.

The Microsoft® implementation preserves the attributes in the order in which they appear in the source. Additional attributes are added to the end of the list. A namespace attribute, xmlns, is inserted as the first in the list if the tag name requires the namespace, or immediately before the attribute that uses an undeclared namespace.

Like the node list, however, a named node map collection is live; that is, the addition and removal of nodes, and changes within nodes, are immediately reflected in the collection. This means that two successive requests for items using the same index can return two different items, depending on changes to the collection. This also means that changes to the node objects are immediately available in the nodes obtained from the list.

When a default value is specified in the schema for an attribute and that attribute is removed, it is automatically regenerated with the default value, as specified in the W3C standard. The attribute reappears immediately because the collection is live, with a corresponding change to the count of items in the collection. When removing an attribute with such a default setting, you must again retrieve the number of items in the collection to obtain an accurate count. You should also reset any index into the collection.

Versioning

MSXML 2.0 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

Header and IDL files: msxml2.h, msxml2.idl

Version-Dependent ProgID: Msxml2.DOMDocument.5.0, Msxml2.FreeThreadedDOMDocument.5.0

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

IXMLDOMNamedNodeMap Members | IXMLDOMNode