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.
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.
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>
Code (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
denotes a Microsoft extension to the W3C DOM.
PROPERTIES
length Property
This is a read-only property that returns the number of nodes in the collection.
Syntax: NamedNodeMap.length
METHODS
getNamedItem Method
This method returns a Node object for the specified item,
or null if the item does not exist.
Syntax: NamedNodeMap.getNamedItem(name)
getQualifiedItem Method
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.
Syntax: NamedNodeMap.getQualifiedItem(baseName, nameSpaceURI)
item Method
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.
Syntax: NamedNodeMap.item.(index)
nextNode Method
This method returns the next node in the collection.
Syntax: NodeList.nextNode( )
removeNamedItem Method
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.
Syntax: NamedNodeMap.removeNamedItem(name)
removeQualifiedItem Method
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.
Syntax: NamedNodeMap.removeQualifiedItem(baseName, nameSpaceURI)
reset Method
This method resets the iterator for the collection.
Syntax: NodeList.reset( )
setNamedItem Method
This method adds the specified item to the collection using its
nodeName property.
Syntax: NamedNodeMap.setNamedItem(newItem)
|