XML technologies / XML / XML in a Nutshell / DOM reference / Object reference / NamedNodeMap
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
The interface NamedNodeMap is a list of named items retrievable by name. Although it has the same methods and attributes as NodeList, the interfaces are unrelated. Nodes in a NamedNodeMap can also be traversed using the attribute length and method item() but their order is undefined.
Java example
// Lies die Attribute des Elements.
NamedNodeMap nnm = elem.getAttribute( );
Attributes
The NamedNodeMap has one attribute:
length: unsigned long
The total number of Node objects in the list.
Java binding
public long getLength( );
Java example
// Iteriere über die gesamte Attributliste.
for (int i = 0; i < nnm.getLength( ); i++) {
...
}
Methods
A NamedNodeMap object defines these methods:
getNamedItem: name
Returns the list item whose attribute nodeName matches the argument name.
Argument
Java binding
public Node getNamedItem(String name);
Java example
// Prüfe, ob ein id-Attribut in der Liste vorhanden ist.
// Erzeuge ein neues, falls notwendig.
// nnm wurde durch Auslesen der Attribute
// aus einem Element erzeugt.
if (nnm.getNamedItem("id") == null) {
// Finde das Dokument.
Document doc = elem.getOwnerDocument( );
// Erzeuge einen neuen Attributknoten.
Attr attrID = doc.createAttribute("id");
// Setze den Attributwert ...
attrID.appendChild(doc.createTextNode(makeUniqueID(elem)));
// ... und füge den Knoten in die NamedNodeMap ein.
nnm.setNamedItem(attrID);
}
getNamedItemNS: namespaceURI, localNam (2)
Extends the method getNamedItem with namespace support. It searches by both local name and namespace URI.
Arguments
namespaceURI: DOMString
Namespace URI of the node to find.
localName: DOMString
Local name of the node to find.
Java binding
public Node getNamedItemNS(String namespaceURI, String localName);
item: index
Returns the Node object at position index. If the index is < 0 or >= the attribute length of the NodeList, the result is null.
Argument
index: unsigned long
The node index, starting at 0.
Java binding
public Node item(long index);
Java example
// Entferne das letzte Attribut aus der Liste.
if (nnm.getLength( ) > 0) {
nnm.removeNamedItem(nnm.item(nnm.getLength()-1).getNodeName( ));
}
removeNamedItem: name
Removes the Node object whose property nodeName matches the parameter name and returns a reference to it. If it is an Attr object and the DTD defines a default attribute value, it is immediately replaced by a new Attr object with that default value.
Argument
name: DOMString
Specifies the
nodeNameof the node to remove.
Exception
NOT_FOUND_ERR
Raised if no matching node exists in the list.
Java binding
public Node removeNamedItem(String name) throws DOMException;
Java example
// Entferne das id-Attribut.
NamedNodeMap nnm = elem.getAttribute( );
if (nnm.removeNamedItem("id") == null) {
System.err.println("Kein id-Attribut vorhanden.");
}
removeNamedItemNS: namespaceURI, localName (2)
Removes the Node object with matching properties namespaceURI and localName and returns its reference. If it is an Attr object and the DTD defines a default, it is immediately replaced by a new Attr object with that default value.
Arguments
namespaceURI: DOMString
Namespace URI of the node to remove.
localName: DOMString
Local name of the node to remove.
Exception
NOT_FOUND_ERR
Raised if the list has no matching parameters
namespaceURIandlocalName.
Java binding
public Node removeNamedItemNS(String namespaceURI, String localName);
setNamedItem: arg
Inserts the specified Node object into the list using the attribute nodeName. Since this value is hardcoded for many node types only one node of each such type fits in the NamedNodeMap. See the description of “nodeName: DOMString.” for details.
The result is the replaced Node object or null, if no node has the specified nodeName.
Argument
arg: Node
The
Nodeobject to store. The attributenodeNameis the search key. An existing node with the samenodeNameas the new node is replaced by the node referenced byarg.
Exceptions
WRONG_DOCUMENT_ERR
Raised if the
argnode comes from a document other than the one that created theNamedNodeMap.NO_MODIFICATION_ALLOWED_ERR
Raised if the
NamedNodeMapis read-only.INUSE_ATTRIBUTE_ERR
Raised if the
argnode is anAttrnode already used by theattributeslist of another element.
Java binding
public Node setNamedItem(Node arg) throws DOMException;
Java example
// Prüfe, ob das id-Attribut bereits vorhanden ist.
// Falls nicht, füge ein neues Attribut ein.
if (nnm.getNamedItem("id") == null) {
// Finde das Dokument.
Document doc = elem.getOwnerDocument( );
// Erzeuge einen neuen Attributknoten.
Attr attrID = doc.createAttribute("id");
// Setze den Attributwert ...
attrID.appendChild(doc.createTextNode(makeUniqueID(elem)));
// ... und baue ihn in die NamedNodeMap ein.
nnm.setNamedItem(attrID);
}
setNamedItemNS: arg (2)
Equivalent to the method setNamedItem, but considers namespaces in the Node object. Returns a reference to any replaced Node object.
Argument
arg: Node
The object to store, of type
Node. Its attributesnamespaceURIandlocalNameare compared with those of existing nodes. A node with matching values is replaced.
Exceptions
WRONG_DOCUMENT_ERR
Raised if the
argnode comes from a document other than the one that created theNamedNodeMap.NO_MODIFICATION_ALLOWED_ERR
Raised if the
NamedNodeMapis read-only.INUSE_ATTRIBUTE_ERR
Raised if the
argnode is anAttrnode already used by theattributeslist of another element.
Java binding
public Node setNamedItemNS(Node arg) throws DOMException;
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de