XML technologies / XML / XML in a Nutshell / The Document Object Model (DOM) / The Node interface and other generic interfaces

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

The interface Node is the starting point in the hierarchy of the DOM core. Although a Node is never instantiated directly, all other interfaces derive from this base interface. This means that the Node interface can provide information about any object in a DOM document tree without knowing its actual type. You can traverse the entire document structure and analyze its contents using only the properties and methods defined by the Node interface. As the following table shows, this interface provides information about the type, position, name, and value of the document data.

Table: The Node interface

Name

Type

Read-only

2.0

3.0

Attribute

attributes

NamedNodeMap

✗

baseURI

DOMString

✗

✗

childNodes

NodeList

✗

firstChild

Node

✗

lastChild

Node

✗

localName

DOMString

✗

✗

namespaceURI

DOMString

✗

✗

nextSibling

Node

✗

nodeName

DOMString

✗

nodeType

unsigned short

✗

nodeValue

DOMString

ownerDocument

Document

✗

✗

parentNode

Node

✗

prefix

DOMString

✗

previousSibling

Node

✗

textContent

DOMString

✗

Methoden

appendChild

Node

cloneNode

Node

compareDocumentPosition

unsigned short

✗

getFeature

DOMObject

✗

getUserData

DOMUserData

✗

hasAttributes

boolean

✗

hasChildNodes

boolean

insertBefore

Node

isDefaultNamespace

boolean

✗

isEqualNode

boolean

✗

isSameNode

boolean

✗

isSupported

boolean

✗

lookupNamespaceURI

DOMString

✗

lookupPrefix

DOMString

✗

normalize

void

✗

removeChild

Node

replaceChild

Node

setUserData

DOMUserData

✗

Since a Node is never created directly, there is an attribute nodeType. This contains a value identifying the actual type of a Node instance. Based on nodeType you can use a cast operator to pass a generic reference to a Node correctly to a specialized interface for further processing. The following table lists the possible values of this attribute and the corresponding DOM interfaces. The table after that summarizes the possible values of the attributes nodeName, nodeValue and attributes.

Table: DOM node types and corresponding interfaces

Value of nodeType

DOM interfaces

ATTRIBUTE_NODE

Attr

CDATA_SECTION_NODE

CDATASection

COMMENT_NODE

Comment

DOCUMENT_FRAGMENT_NODE

DocumentFragment

DOCUMENT_NODE

Document

DOCUMENT_TYPE_NODE

DocumentType

ELEMENT_NODE

Element

ENTITY_NODE

Entity

ENTITY_REFERENCE_NODE

EntityReference

NOTATION_NODE

Notation

PROCESSING_INSTRUCTION_NODE

ProcessingInstruction

TEXT_NODE

Text

Table: DOM node types and method return values

Value of nodeType

nodeName

nodeValue

Attributes

ATTRIBUTE_NODE

Attributname

Attributwert

null

CDATA_SECTION_NODE

#cdata-section

content

null

COMMENT_NODE

#comment

content

null

DOCUMENT_FRAGMENT_NODE

#document-fragment

null

null

DOCUMENT_NODE

#document

null

null

DOCUMENT_TYPE_NODE

Dokumenttyp-Name

null

null

ELEMENT_NODE

Tag-Name

null

NamedNodeMap

ENTITY_NODE

Entity-Name

null

null

ENTITY_REFERENCE_NODE

Name des referenzierten Entities

null

null

NOTATION_NODE

Notationsname

null

null

PROCESSING_INSTRUCTION_NODE

Ziel

Inhalt ohne Ziel

null

TEXT_NODE

#text

content

null

Note that the attribute nodeValue returns the contents of ordinary text and comment nodes, but nothing for elements. Before DOM Level 3, all Text child nodes within an element had to be located before its text content could be retrieved. DOM Level 3, however, introduced the new functions getTextContent( ) and setTextContent( ) which make this more convenient.

The NodeList interface

The interface NodeList provides access to the ordered contents of a node. It is most often used to obtain the text nodes and child elements of an element node. The following table provides a Summary of the interface NodeList.

Table: The NodeList interface

Name

Type

Read-only?

2.0

3.0

Attribute

length

Long

✗

Methoden

item

Node

The interface NodeList is fundamental and is generally combined with a loop to iterate over all children of a node, as in the following example:

NodeList nl = nd.getChildNodes(  );
for (int i = 0; i < nl.getLength(  ); i++) {
  Node ndChild = nl.item(i);
  if (ndChild.getNodeType(  ) =  = Node.COMMENT_NODE) {
    System.out.println("Kommentar gefunden: " + ndChild.getNodeValue(  ));
  }
}

The NamedNodeMap interface

The interface NamedNodeMap is used to create unordered collections whose contents are identified by name. In practice, it provides access to the attributes of an element. The following table summarizes the interface NamedNodeMap.

Table: The NamedNodeMap interface

Name

Type

Read-only?

2.0

3.0

Attribute

length

Long

✗

Methoden

getNamedItem

Node

getNamedItemNS

Node

✗

removeNamedItem

Node

removeNamedItemNS

Node

setNamedItem

Node

setNamedItemNS

Node

✗

Document structure and Node

Although DOM does not specify a connection to the parser, it precisely defines how a document’s syntactic structures are mapped to DOM objects. A document is represented as a hierarchical tree, with every node linked to its parent, children, and siblings:

 <beispiel name="wert"><textknoten>Testdaten</textknoten></beispiel> 

The figure The document in memory with links shows how a DOM parser represents this small example document.

Every object derived from Node in a DOM document contains references to its parent, children, and siblings. These references allow an application to use standard tree algorithms to traverse the parts of the document. Traversing the tree is a common approach to finding information in a DOM. The code example A furniture checker application on the page A simple DOM application demonstrates how this works.

  

 

 

 

back to top