- Browsing DOM Tree Structure

This section provides a tutorial example on how to write a DOM object browser,, to browse through the DOM object tree structure and print the content at each tree node.

In DOM, an XML file is represented with a tree structure, called "document". Every piece of information in an XML file is abstracted as an org.w3c.dom.Node object, and represented by a node in the tree.

"Node" is actually an interface. It is implemented into many DOM classes to represent different types of information in an XML file. Features that are common to DOM classes are defined as methods in the Node interface. Major get methods of Node include:

Here is is a list of node types that are supported by DOM:


The following program illustrates how an XML file can be parse into a DOM document tree, and how get methods of Node can be used to browse the tree:

 * Copyright (c) All Rights Reserved.
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
class DOMBrowser {
   public static void main(String[] args) {
      try {
      	 File x = new File(args[0]);
         DocumentBuilderFactory f 
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder b = f.newDocumentBuilder();
         Document d = b.parse(x);
         printNode(d, "");
      } catch (ParserConfigurationException e) {
      } catch (SAXException e) {
      } catch (IOException e) {
   static void printNode(Node n, String p) {
      NodeList l = n.getChildNodes();
      NamedNodeMap m = n.getAttributes();
      int ml = -1;
      if (m!=null) ml = m.getLength(); 
      System.out.println(p+n.getNodeName()+": "+n.getNodeType()+", "
         +l.getLength()+", "+ml+", "+n.getNodeValue());
      for (int i=0; i<ml; i++) {
         Node c = m.item(i);
         printNode(c,p+" |-");
      for (int i=0; i<l.getLength(); i++) {
         Node c = l.item(i);
         printNode(c,p+" ");

Now let's use this program to browse my first XML file, hello.xml:

<?xml version="1.0"?>
<body>Hello world!</body>

You will get the following output:

C:\herong>java DOMBrowser hello.xml

#document: 9, 1, -1, null
 body: 1, 1, 0, null
  #text: 3, 0, -1, Hello world!

Here is how to read the output:

Here is another XML file with more elements, user.xml:

<?xml version="1.0"?>
<user status="active">
 <!-- This is not a real user. -->

Run DOMBrowser with this XML file JDK 12, I got:

C:\herong>\progra~1\java\jdk-12.0.1\bin\java DOMBrowser user.xml

#document: 9, 1, -1, null
 user: 1, 7, 1, null
  |-status: 2, 1, -1, active
  |- #text: 3, 0, -1, active
  #text: 3, 0, -1,

  #comment: 8, 0, -1,  This is not a real user.
  #text: 3, 0, -1,

  first_name: 1, 1, 0, null
   #text: 3, 0, -1, John
  #text: 3, 0, -1,

  last_name: 1, 1, 0, null
   #text: 3, 0, -1, Smith
  #text: 3, 0, -1,

The output is more interesting:

But I ran DOMBrowser in JDK 1.8, I got:

C:\herong>\progra~1\java\jdk1.8.0\bin\java DOMBrowser user.xml

#document: 9, 1, -1, null
 user: 1, 7, 1, null
  |-status: 2, 0, -1, active
  #text: 3, 0, -1,

  #comment: 8, 0, -1,  This is not a real user.
  #text: 3, 0, -1,

  first_name: 1, 1, 0, null
   #text: 3, 0, -1, John
  #text: 3, 0, -1,

  last_name: 1, 1, 0, null
   #text: 3, 0, -1, Smith
  #text: 3, 0, -1,

The attribute is represented only once as "attribute" node with no child nodes. So the DOM specifications were different between JDK 1.8 and JDK 10.


Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

DOM (Document Object Model) - API for XML Files - Parsing XML Files with DOM - Browsing DOM Tree Structure - Building a New DOM Document - Converting DOM Documents to XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB