Java API for XML (JAXP) – DOM

What is DOM

DOM is an Object representation of an XML, HTML or XHTML document. In this tutorial we will be dealing with only XML. DOM represents the XML as a Document tree. JAXP provides API for DOM implementation in Java. It also provides parsing interface which can be used to plugin different parsers (JAXP provides a default implementation)


  • org.w3c.dom – Contains classes that are DOM representation of an XML Document and its components. Classes include :
    • Document – Represents an entire XML or HTML Document. It is the root of the Document tree.
    • Element – Represents an element in an XML or HTML Document. It has methods to access the attributes of an xml element.
    • Attribute – Represents an attribute in an Element object.
    • CDATASection – Represents CDATA Section. These are blocks of text that can contain characters that are normally part of markup.
    • Text – Represents textual content of an element or an Attribute. If the text does not contain markup then all text is contain in a single node, if it contains markup then the various elements are added as children of the Text element.
    • Processing Instruction – Represents a Processing Instruction in an XML document.
    • Comment – Represents a comment in an XML Document. Contains comment text.
  • javax.xml.parsers – Contains interfaces that the DOM and SAX Parsers need to implement :
    • DocumentBuilderFactory – Defines a factory that can be used to obtain DOM parsers
    • DocumentBuilder – Defines interface methods that can be used to obtain a DOM Object tree from an XML Document

JAXP DOM in action

Lets now see an example of a DOM representation of an XML document. In this example we look at the following:

  • Parsing the XML using the default DOM Parser.
  • Obtaining the root element
  • Obtaining all elements with a specific name
  • Obtaining all elements with a specific name and in a specific namespace
  • Iterating through all child nodes and parsing through them.

Java API for XML (JAXP) – DOM

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top