XML DOM: The NamedNodeMap Object

The NamedNodeMap Object in XML DOM represents a collection of attribute nodes associated with an element. It is not an array but functions like one, allowing you to access, retrieve, and manipulate attributes of an XML element programmatically.

This guide, brought to you by The Coding College, will help you understand how to use the NamedNodeMap Object effectively for managing attributes in an XML document.

What Is a NamedNodeMap Object?

A NamedNodeMap is a collection of attribute nodes (name-value pairs) of an element node in an XML document. Unlike a NodeList, NamedNodeMap is used specifically for attributes, providing access to them by name or index.

Example:

Given the following XML:

<book title="XML Basics" author="John Doe" year="2024" />

The attributes of the <book> element (title, author, year) are stored in a NamedNodeMap.

Key Features of NamedNodeMap

FeatureDescription
Dynamic NatureNamedNodeMap reflects changes to the attributes dynamically.
Indexed AccessAttributes can be accessed using a zero-based index.
Keyed AccessAttributes can also be accessed by their name (e.g., node.getNamedItem(name)).

How to Retrieve a NamedNodeMap

You can retrieve a NamedNodeMap using the attributes property of an XML element node.

Example:

const attributes = xmlDoc.getElementsByTagName("book")[0].attributes;
console.log(attributes); // Outputs the NamedNodeMap object of the <book> element.

NamedNodeMap Properties

PropertyDescription
lengthReturns the number of attributes in the NamedNodeMap.
[index]Accesses an attribute node by its index.

NamedNodeMap Methods

MethodDescription
getNamedItem(name)Returns the attribute node with the specified name.
setNamedItem(attribute)Adds or updates an attribute node in the NamedNodeMap.
removeNamedItem(name)Removes the attribute node with the specified name.
item(index)Returns the attribute node at the specified index.

Working with NamedNodeMap

Example 1: Accessing Attributes

Retrieve all attributes of an element:

const bookElement = xmlDoc.getElementsByTagName("book")[0];
const attributes = bookElement.attributes;

for (let i = 0; i < attributes.length; i++) {
  console.log(attributes[i].name + ": " + attributes[i].value);
}
// Outputs:
// title: XML Basics
// author: John Doe
// year: 2024

Example 2: Accessing an Attribute by Name

const bookElement = xmlDoc.getElementsByTagName("book")[0];
const titleAttribute = bookElement.attributes.getNamedItem("title");
console.log(titleAttribute.value); // Outputs: "XML Basics"

Example 3: Adding or Updating an Attribute

Add a new attribute or update an existing one:

const bookElement = xmlDoc.getElementsByTagName("book")[0];
const newAttr = xmlDoc.createAttribute("publisher");
newAttr.value = "The Coding College";
bookElement.attributes.setNamedItem(newAttr);

console.log(bookElement.getAttribute("publisher")); // Outputs: "The Coding College"

Example 4: Removing an Attribute

Remove an attribute by name:

const bookElement = xmlDoc.getElementsByTagName("book")[0];
bookElement.attributes.removeNamedItem("year");

console.log(bookElement.hasAttribute("year")); // Outputs: false

Practical Applications

  1. Attribute Validation:
    • Verify if specific attributes exist and check their values.
  2. Dynamic Attribute Management:
    • Add, update, or remove attributes dynamically in XML documents.
  3. Data Transformation:
    • Use NamedNodeMap to modify or standardize attributes for XML processing tasks.
  4. API Development:
    • Manipulate attributes in XML data for API responses or requests.

NamedNodeMap: Summary

FeatureDescription
What is it?A collection of attribute nodes of an XML element.
Key MethodsgetNamedItem(), setNamedItem(), removeNamedItem(), and item(index).
Common Use CasesAttribute validation, dynamic updates, and data transformation.

Tips for Using NamedNodeMap

  1. Iterate Carefully:
    • Always use the length property to avoid index out-of-bounds errors.
  2. Access by Name When Possible:
    • Use getNamedItem() for specific attributes to improve clarity and performance.
  3. Combine with DOM Manipulation:
    • Use NamedNodeMap alongside other DOM methods for comprehensive XML processing.

For more tutorials and examples on XML DOM manipulation, visit The Coding College and take your XML skills to the next level.

Leave a Comment