The Element object is one of the most common types of Node
that will be encountered in a document tree. An Element may have attributes, but
these are considered to be properties of the Element rather than children. The
Element object, therefore, provides a number of methods designed to make the handling
of attributes easier.
To illustrate the Element object, the following code uses the
getElementsByTagName method to get all the
elements in the 'staff.xml' file, and then iterates through the resulting
NodeList collection displaying the
tagName of each.
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>
Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("staff.xml")
Set NodeList = objXMLDoc.getElementsByTagName("*")
For Each Elem In NodeList
document.write(Elem.tagName & "<br>")
Next
Output:
staff
employee
f_name
l_name
employee
f_name
l_name
An Element is also a Node object, and so inherits
various properties and methods from it. For details of the values returned by the
nodeName,
nodeType and nodeValue
properties for an Element, see the Node object.
denotes a Microsoft extension to the W3C DOM.
PROPERTIES
attributes Property
This is a read-only property that returns an
NamedNodeMap for nodes that can have
attributes.
Syntax: Node.attributes
baseName Property
This is a read-only property that returns the base name for a node.
Syntax: Node.baseName
childNodes Property
This is a read-only property containing a node list of all children for those elements that
can have them.
Syntax: Node.childNodes
dataType Property
This is a read-only property that specifies the data type for the node.
Syntax: Node.dataType
definition Property
This property returns the definition of the node in the DTD or schema.
Syntax: Node.definition
firstChild Property
This is a read-only property that returns the first child node of a node. If there is none,
it returns null.
Syntax: Node.firstChild
lastChild Property
This is a read-only property that returns the last child node of a node. If there is none,
it returns null.
Syntax: Node.lastChild
namespaceURI Property
This property is read-only and returns the URI (Universal Resource Indentifier) of the
namespace.
Syntax: Node.namespaceURI