IXMLDOMNodeList

MSXML 5.0 SDK

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

IXMLDOMNodeList

Supports iteration through the live collection, in addition to indexed access.

[Script]

Example

The following script example creates an IXMLDOMNodeList object by using the document's getElementsByTagName method.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var objNodeList;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   objNodeList = xmlDoc.getElementsByTagName("author");
   alert(objNodeList.length);
   for (var objNode in objNodeList)
      alert(objNode.xml);
}
[Visual Basic]

Example

The following Microsoft® Visual Basic® example creates an IXMLDOMNodeList object by using the document's getElementsByTagName method, and examines the content of each node.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim objNodeList As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
xmlDoc.Load ("books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set objNodeList = xmlDoc.getElementsByTagName("author")
   MsgBox objNodeList.length
   For Each objNode In objNodeList
      MsgBox objNode.xml
   Next
End If
[C/C++]

Example

The following C/C++ example gets all elements with node name AUTHOR and displays the number of nodes found with the following sample.

<?xml version='1.0'?>
<COLLECTION
   xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
  <BOOK>
    <TITLE>Lover Birds</TITLE>
    <AUTHOR>Cynthia Randall</AUTHOR>
    <PUBLISHER>Lucerne Publishing</PUBLISHER>
  </BOOK>
  <BOOK>
    <TITLE>The Sundered Grail</TITLE>
    <AUTHOR>Eva Corets</AUTHOR>
    <PUBLISHER>Lucerne Publishing</PUBLISHER>
  </BOOK>
  <BOOK>
    <TITLE>Splish Splash</TITLE>
    <AUTHOR>Paula Thurman</AUTHOR>
    <PUBLISHER>Scootney</PUBLISHER>
  </BOOK>
</COLLECTION>

#import "msxml5.dll"
using namespace MSXML2;

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

void XMLDOMNodeListSample()
{
   try {
      IXMLDOMDocumentPtr docPtr;
      IXMLDOMNodeListPtr NodeListPtr;
      IXMLDOMNodePtr DOMNodePtr;

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

      // load a document
      _variant_t varXml("C:\\book.xml");
      _variant_t varOut((bool)TRUE);
      varOut = docPtr->load(varXml);
      if ((bool)varOut == FALSE)
         throw(0);
      NodeListPtr = docPtr->getElementsByTagName("AUTHOR");
      MessageBox(NULL, _bstr_t(NodeListPtr->length), _T("Node List length"), MB_OK);
   } catch(...)
   {
      MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
   }
   CoUninitialize();
}

Remarks

A NodeList 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.

Versioning

MSXML 2.0 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

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

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

IXMLDOMNodeList Members | IXMLDOMNode