XML DOM getAttribute() Methode
Definition and usage
The getAttribute() method retrieves the value of the attribute by name.
Syntax:
elementNode.getAttribute(name)
Parameters | Description |
---|---|
name | Required. Specifies the attribute from which the attribute value is obtained. |
Tips and comments
For XML documents using namespaces, it is necessary to use getAttributeNS() method.
Example
In all examples, we will use the XML file books.xml, and JavaScript function loadXMLDoc().
The following code snippet retrieves the values of the "category" attribute of all <book> elements:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for (i=0;i<x.length;i++)
{
document.write(x[i].getAttribute('category')
);
document.write("<br />");
}
The output of the above code is:
COOKING CHILDREN WEB WEB