Document Object Model (DOM)


 

Overview

A Document Object Model is a tree structure containing element and text nodes that reference XML components. DOM functions let one create and remove nodes, change their contents, and traverse the node hierarchy. Text and elements are intermixed in a DOM hierarchy, in what is called a mixed-content model. If you are dealing with simple structures, and are not using XML Schema, use JDOM and dom4j.

Here is a typical line one might find in an XML document:

<line>The cow jumped <b>over</b> the moon. </line>

Here is how it would look rendered into a DOM hierarchy of nodes:

ELEMENT: line
  +  TEXT: The cow jumped 
  +  ELEMENT: b
    + TEXT: over
  + TEXT: the moon.

The DOM Node API defines methods to derive values for node values, names, and type. For the first element node above, the nodeName() method "line", and nodeValue() returns null. For the first text node, nodeName() returns "#text", and nodeValue() returns "The cow jumped".

You can transform a DOM into XML using Xalan.

 

Alternatives to DOM

For very simple XML data structures one alternative is to use is the java.util.reqex package.

For simple XML structures that do not contain mixed-content, one can JDOM and dom4j, which are able parse XML structures that contain text only. For example:

<animals>
  <type> Siberian Tiger
    <found> Siberia </found>
  </type>
   ...
</animals>

 

Complex XML Docs

Here is an example of complex XML data:

<line>
  The &xproject; <![CDATA[<i>projectname</i>]]> is
  <?display: red><bold>vital</bold><?display: normal>.
</line>

Here is the DOM structure:

+ ELEMENT: line
  + TEXT: The   + ENTITY REF: xproject
    + COMMENT: Black ops project for super-secret division
    + TEXT: Ice-9
  + CDATA: <i>projectname</i>
  + TEXT: is
  + PI: display: red
  + ELEMENT: bold
    + TEXT: vital
  + PI: display: normal

 

Skeleton DOM Program

The DomEcho01.java app contains a JAXP skeleton template that uses the org.w3c.dom.Document class to create a DOM.

 

Display a JTree

The DomEcho02.java program converts a DOM into a JTreeModel and display the full DOM in a JTree.

 

Node Types

Node nodeName() nodeValue() attributes nodeType()
Attr name of attribute value of attribute null 2
CDATASection #cdata-section content of the CDATAsection null 4
Comment #comment content of the comment null 8
Document #document null null 9
DocumentFragment #document-fragment null null 11
DocumentType document type name null null 10
Element tag name null NamedNodeMap 1
Entity entity name null null 6
EntityReference name of entity referenced null null 5
Notation notation name null null 12
ProcessingInstruction target entire contentexcluding thetarget null 7
Text #text content of the text node null 3

 

Compressing a Tree View

Use DomEcho03.java to compress a Tree View

 

Acting on Tree Selections

To concatenate the subtrees under selected nodes to display them in the htmlPane use DomEcho04.java.

To test, run against slideSample10.xml.

 

Obtaining a DOM from the Factory

Use DomEcho05.java to create a document builder factory by a creating a new DOM instead of parsing an existing XML document.

Run the program with no arguments.

 

Normalisation

Use DomEcho06.java to normalise a DOM.

 


Further Information

  1. Understanding the TreeModel
  2. The DOM standard page
  3. The W3C standard validation mechanism: XML Schema
  4. RELAX NG's regular-expression based validation mechanism
  5. Schematron's assertion-based validation mechansim


  Home