item Method (IXMLDOMNamedNodeMap)
Allows random access to individual nodes within the collection.
JScript Syntax
var objXMLDOMNode = oXMLDOMNamedNodeMap.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 IXMLDOMNamedNodeMap
object to retrieve the attributes for an element node selected using the SelectSingleNode
method. It then iterates through the attributes, before displaying the name and value of each attribute in the collection.
Note
You can use books.xml to run this sample code.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var oNamedNodeMap, nodeBook, str;
var str = "";
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
xmlDoc.setProperty("SelectionLanguage", "XPath");
nodeBook = xmlDoc.selectSingleNode("//book");
oNamedNodeMap = nodeBook.attributes;
for (var i=0; i<oNamedNodeMap.length; i++) {
str += "Attr" + i + " name: " +
oNamedNodeMap.item(i).name + "\n" +
"Attr" + i + " value: " +
oNamedNodeMap.item(i).text + "\n";
}
WScript.Echo(str);
}
Output
Attr0 name: id
Attr0 value: bk101
C/C++ Syntax
HRESULT get_item(
long index,
IXMLDOMNode **listItem);
Parameters
index
[in]
The index of the item within the collection. The first item is zero.
listItem
[out, retval]
The IXMLDOMNode
object. Returns Null if the index is out of range.
Return Values
S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the listItem
parameter is Null.
Versioning
Implemented in: MSXML 3.0 and MSXML 6.0