XML DOM getAttributeNode() Method

Definition and Usage

The getAttributeNode() method retrieves an attribute node from the current element by name.

Syntax:

elementNode.getAttributeNS(ns, name)
Parameter Description
name Required. Specifies the attribute node to be obtained.

Description

This method returns an Attr node representing the specified attribute and value. Please note that the attribute node can also be obtained from the attributes property inherited from the Node interface.

Example

In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().

The following example retrieves the "category" attribute from all <book> elements in "books.xml":

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++)
{
attnode=x.item(i).getAttributeNode("category");
document.write(attnode.name);
document.write(" = ");
document.write(attnode.value);
document.write("<br />");
}

The output of the above code is:

category = COOKING
category = CHILDREN
category = WEB
category = WEB