2.3 Element

The Element interface inherits from the Node interface.

By far the vast majority of objects (apart from text) that authors encounter when traversing a document are Element nodes. Assume the following XML document:

 <elementExample id="demo">  
    <subelement1/>  
    <subelement2>
       <subsubelement/>
    </subelement2>
 </elementExample>  
 

When represented using DOM, the top node is an Element node for “elementExample,” which contains two child Element nodes, one for “subelement1” and one for “subelement2”. The “subelement1” node contains no child nodes.

Elements may have attributes associated with them. Since the Element interface inherits from Node, the generic Node interface method getAttributes may be used to retrieve the set of all attributes for an element. The Element interface contains methods to retrieve either an Attr object by name or an attribute value by name. In XML, where an attribute value may contain entity references, an Attr object should be retrieved to examine the possibly fairly complex sub-tree representing the attribute value. On the other hand, in HTML, where all attributes have simple string values, methods to directly access an attribute value can safely be used as a convenience.