XML DOM » Objects » NamedNodeMap

The NamedNodeMap object represents a collection of nodes that can be accessed by name. Although the nodes in the collection can also be accessed through an ordinal index, they are not maintained in any particular order, and the indexing serves merely to provide convenient enumeration of the collection. It is similar to the NodeList Object in that it also represents a live collection of Node objects which will immediately reflect any alteration to the number or properties of the nodes in it. An instance of it can be created from the document's attributes property.

Examples

Code:
XML:

<States>
   <State ref="FL" const="27">
      <name>Florida</name>
      <capital>Tallahassee</capital>
   </State>
   <State ref="IA" const="29">
      <name>Iowa</name>
      <capital>Des Moines</capital>
   </State>
</States>

JavaScript:

var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("library.xml");

var attr = xml_doc.documentElement.childNodes.item(0).attributes;
var n_attr = attr.length
for (i = 0; i < n_attr; i++)
   document.write(attr.item(i).name + ": " + attr.item(i).text + "<br>");
Output:
ref: FL
const: 27
Explanation:

Using the 'states.xml' file we shall first create a NamedNodeMap object of all the attribute nodes of the first State element and then iterate through the collection using the item method to display the attribute name and associated text.

Language(s): JavaScript XML

Properties

length

Syntax: object.length

This is a read-only property that returns the number of nodes in the collection.

Methods

getNamedItem

Syntax: namedNodeMap.getNamedItem(name)

This method returns a Node object for the specified item, or null if the item does not exist.

getQualifiedItem

Syntax: object.getQualifiedItem(baseName, nameSpaceURI)

This method returns the attribute with the specified namespace and attribute name. The 'nameSpaceURI' parameter is the nameSpace prefix that qualifies the attribute name. Returns null if the item is not an attribute, or is not in the collection. nameSpaceURI)

item

Syntax: object.item.(index)

This method returns the item at the specified index of the collection. These are numbered from 0 to one less than the value of the length property. Using an invalid index returns null.

nextNode

Syntax: nodeList.nextNode( )

This method returns the next node in the collection.

removeNamedItem

Syntax: namedNodeMap.removeNamedItem(name)

This method removes the specified item from the collection and returns it as a Node object, or returns null if the item does not exist.

removeQualifiedItem

Syntax: object.removeQualifiedItem(baseName, nameSpaceURI)

This method removes the attribute with the specified namespace and attribute name. The 'nameSpaceURI' parameter is the nameSpace prefix that qualifies the attribute name. Returns the node removed, or null if no node was removed. nameSpaceURI)

reset

Syntax: nodeList.reset()

This method resets the iterator for the collection.

setNamedItem

Syntax: namedNodeMap.setNamedItem(newItem)

This method adds the specified item to the collection using its nodeName property.

See Also: