item Method (IXMLDOMNodeList)

MSXML 5.0 SDK

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

item Method (IXMLDOMNodeList)

Allows random access to individual nodes within the collection.

[Script]

Script Syntax

var objXMLDOMNode = oXMLDOMNodeList.item(index);

Parameters

index
A long integer. An index of the item within the collection. The first item is zero.

Return Value

An object. Returns IXMLDOMNode. Returns Null if the index is out of range.

Example

The following script example creates an IXMLDOMNodeList object with the document's getElementsByTagName method. It then iterates through the collection, displaying the text value of each item in the list.

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

Visual Basic Syntax

Set objXMLDOMNode = oXMLDOMNodeList.item(index)

Parameters

index
A long integer. An index of the item within the collection. The first item is zero.

Return Value

An object. Returns IXMLDOMNode. Returns Null if the index is out of range.

Example

The following Microsoft® Visual Basic® example creates an IXMLDOMNodeList object with the document's getElementsByTagName method. It then iterates through the collection, displaying the text value of each item in the list.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim objNodeList As IXMLDOMNodeList
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 objNodeList = xmlDoc.getElementsByTagName("author")
   For i = 0 To (objNodeList.length - 1)
      MsgBox objNodeList.Item(i).Text
   Next
End If
[C/C++]

C/C++ Syntax

HRESULT get_item(
  long index,
  IXMLDOMNode **listItem);

Parameters

index [in]
An index of the item within the collection. The first item is number zero.
listItem [out, retval]
An IXMLDOMNode. Returns Null if the index is out of range.

C/C++ Return Values

S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the listItem parameter is Null.

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: IXMLDOMNodeList | IXMLDOMSelection