XML DOM » Attr » value

Syntax:
attr.value

This property returns the value for the attribute.

This will either be the value specified in the XML file or, if there isn't one but the attribute has a default value assigned in a DTD, a default value. This property is returned as a string with character and general entity references being replaced with their values. The following example uses the 'staff.xml' file and obtains a NamedNodeMap of all the attributes of the first child element of the document's root node. It then iterates through them displaying their values.

Examples

Code:
XML:

<staff>
   <employee ssn="123456" pay="3">
      <f_name>John</f_name>
      <l_name>Sullivan</l_name>
   </employee>
   <employee ssn="987654" pay="2">
      <f_name>Mary</f_name>
      <l_name>Lopez</l_name>
   </employee>
</staff>

JavaScript:

xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("staff.xml");

employee = xml_doc.documentElement.firstChild;
atts = employee.attributes;
n_atts = atts.length;
for (i = 0; i < n_atts; i++)
   document.write(atts[i].value + "<br>");
Output:
123456
3
Explanation:

This example uses the 'staff.xml' file and obtains a NamedNodeMap of all the attributes of the first child element of the document's root node. It then iterates through them displaying their values.

Language(s): JavaScript XML