Object: NamedNodeMap
The NamedNodeMap object represents collections of nodes that can be accessed by name.
You can access an individual node within a collection by its index position using NamedNodeMap.item(), by its name or id attribute using NamedNodeMap.getNamedItem(), or you can index the object directly. For example, if you create an instance of a NamedNodeMap object called myNamedNodeMap, then specifying
NamedNodeMap properties
NamedNodeMap methods
Method name |
Description |
Availability |
|---|---|---|
Retrieves the node with the given name. |
4.6 or later |
|
Retrieves the node with the given name and namespace URI. |
4.6 or later |
|
Returns the node at the given index in the map. |
4.6 or later |
|
Removes the node with the given name from the map. |
4.6 or later |
|
Removes the node with the given name and namespace URI from the map. |
4.6 or later |
|
Adds a node using the Node.nodeName attribute. |
4.6 or later |
|
Adds a node using the Node.nodeName attribute and the namespace URI. |
4.6 or later |
Method: NamedNodeMap.getNamedItem()
Parameters
Parameter |
Type |
Description |
|---|---|---|
name |
String |
The value of the Node.nodeName property of the node to retrieve. |
Return values
Returns the node with a Node.nodeName value of name, or null if the given parameter does not identify any existing node in the map.
Method: NamedNodeMap.getNamedItemNS()
Method: NamedNodeMap.item()
Parameters
Parameter |
Type |
Description |
|---|---|---|
index |
int |
An integer that represents the node’s position in the node map. Valid values are between 0 and NamedNodeMap.length-1 inclusive. |
Method: NamedNodeMap.removeNamedItem()
The removeNamedItem() method removes the node with the specified name from the map.
If you are removing an Attr node from the NamedNodeMap.length object that is known to have a default value, the removed Attr will immediately be reattached to the element with its default value, as well as its corresponding namespace URI, local name, and prefix, if applicable.
Parameters
Parameter |
Type |
Description |
|---|---|---|
name |
String |
The value of Node.nodeName property of the node to remove. |
Exceptions
Exception |
Description |
|---|---|
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the NamedNodeMap object is read only. |
NOT_FOUND_ERR |
This error is thrown if no node with a Node.nodeName value of name exists in the NamedNodeMap. |
Method: NamedNodeMap.removeNamedItemNS()
The removeNamedItemNS() method removes the node with the specified name and namespace URI from the map.
If you are removing an Attr node from the NamedNodeMap object that is known to have a default value, the Attr will immediately be reattached to the element with its default value, as well as its corresponding namespace URI, local name, and prefix, if applicable.
Exceptions
Exception |
Description |
|---|---|
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the NamedNodeMap object is read only. |
NOT_FOUND_ERR |
This error is thrown if no node with a Node.nodeName value of name exists in the NamedNodeMap. |
NOT_SUPPORTED_ERR |
This error is thrown if the document does not support XML namespaces. |
Method: NamedNodeMap.setNamedItem()
The setNamedItem() method adds a node to the node map using the Node.nodeName property. If a node with the same Node.nodeName value already exists in the map, the existing node is removed and is replaced by the new node and the removed node is returned.
As the Node.nodeName property is used to store the new node in the map, in order to prevent name clashes, you may not add multiple nodes of those node types that always have the same Node.nodeName value. For example, a text node will always have a Node.nodeName value of #text. Therefore, only a single text node may be included in any NamedNodeMap object.
Return values
If the new node has replaced an existing node, returns the removed node; otherwise returns null.
Exceptions
Exception |
Description |
|---|---|
HIERARCHY_REQUEST_ERR |
This error is thrown if the arg node is not permitted in this NamedNodeMap, for example, if you tried to insert a node other than an Attr into an Element’s map of attributes. |
INUSE_ATTRIBUTE_ERR |
This error is thrown if arg is an Attr object that is already an attribute of another Element object. To add an attribute used elsewhere, you must clone it, and then add it. |
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the NamedNodeMap object is read only. |
WRONG_DOCUMENT_ERR |
This error is thrown if arg was created from a different document than the one that created this NamedNodeMap object. |
Method: NamedNodeMap.setNamedItemNS()
The setNamedItemNS() method adds a node using the Node.nodeName property and the namespace URI.
Return values
If the new node has replaced an existing node, returns the removed node; otherwise returns null.
Exceptions
Exception |
Description |
|---|---|
HIERARCHY_REQUEST_ERR |
This error is thrown if the arg node is not permitted in this NamedNodeMap, for example, if you tried to insert a node other than an Attr into an Element’s map of attributes. |
INUSE_ATTRIBUTE_ERR |
This error is thrown if arg is an Attr object that is already an attribute of another Element object. To add an attribute used elsewhere, you must clone it, and then add it. |
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the NamedNodeMap object is read only. |
NOT_SUPPORTED_ERR |
This error is thrown if the document does not support XML namespaces. |
WRONG_DOCUMENT_ERR |
This error is thrown if arg was created from a different document than the one that created this NamedNodeMap object. |