The Attr interface represents an attribute of an Element
object. Allowable values for this object are usually defined in some sort of document type
definition. The Attr object is also a Node and as such
inherits a number of that object's properties and methods. However, an attribute is not
considered by the DOM to be a child node of an element, but rather a property of it, and so
cannot have a value other than null for the
parentNode,
previousSibling or
nextSibling properties.
The value of an attribute is represented by the child nodes because the value can contain
entity references. Where this is the case the child list will contain both
text and entityReference
nodes. Because the attribute type may be unknown, there are no tokenized attribute values.
The following example uses the 'states.xml' file and creates a NamedNodeMap
of the attributes of the first child of the root element. The code then
displays their name and value
properties.
XML:
<States>
<State ref="FL" const="27">
<name>Florida</name>
<capital>Tallahassee</capital>
</State>
<State ref="IA" const="29">
<name>Iowa</name>
<capital>Des Moines</capital>
</State>
</States>
Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("states.xml")
Set objFirstChild = objXMLDoc.documentElement.firstChild
Set objAttributes = objFirstChild.attributes
For Each Attr in objAttributes
document.write(Attr.name & " ")
document.write(Attr.value & "<br>")
Next
Output:
ref FL
const 27