Share via


IXMLDOMNodeList

 

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

JScript Example

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

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");  
var objNodeList;  
xmlDoc.load("books.xml");  
if (xmlDoc.parseError.errorCode != 0) {  
   var myErr = xmlDoc.parseError;  
   WScript.Echo("You have error " + myErr.reason);  
} else {  
   objNodeList = xmlDoc.getElementsByTagName("author");  
   var str = "The node list has " + objNodeList.length + " items.\n";  
   for (var i=0; i < objNodeList.length; i++) {  
      str = str + (i + 1) + ": " + objNodeList.item(i).xml + "\n";  
   }  
   WScript.Echo(str);  
}  
  

Output

The node list has 12 items.  
1: <author>Gambardella, Matthew</author>  
2: <author>Ralls, Kim</author>  
3: <author>Corets, Eva</author>  
4: <author>Corets, Eva</author>  
5: <author>Corets, Eva</author>  
6: <author>Randall, Cynthia</author>  
7: <author>Thurman, Paula</author>  
8: <author>Knorr, Stefan</author>  
9: <author>Kress, Peter</author>  
10: <author>O'Brien, Tim</author>  
11: <author>O'Brien, Tim</author>  
12: <author>Galos, Mike</author>  

C/C++ Example

The following C/C++ example uses book.xml (below). The example gets all elements with node name AUTHOR and displays the number of nodes found in the XML document.

XML File (book.xml)

<?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>  
  

CPP File (XMLDOMNodeListSample.cpp)

#include “msxml6.h”  
  
inline void TESTHR( HRESULT _hr )   
   { if FAILED(_hr) throw(_hr); }  
  
//XMLDOMNodeList Sample  
int main(int argc, char* argv[])   
{  
    try {  
        MSXML2::IXMLDOMDocumentPtr docPtr;  
        MSXML2::IXMLDOMNodeListPtr NodeListPtr;  
  
        //init  
        TESTHR(CoInitialize(NULL));   
        TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.6.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.

Requirements

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml6.dll, msxml6.lib (MSXML 6.0)

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

Versioning

Implemented in: MSXML 3.0, MSXML 6.0

See Also

IXMLDOMNodeList Members
IXMLDOMNode