XML Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

DOMBrowser.java - DOM Interface Java Example

This section provides a tutorial example on how to parse an XML file and print it back in a tree format using DOM interface in Java - JDK 1.4.1.

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

/**
 * DOMBrowser.java
 * Copyright (c) 2002 by Dr. Herong Yang. http://www.herongyang.com/
 */
import java.io.*;
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) {
         System.out.println(e.toString());      
      } catch (SAXException e) {
         System.out.println(e.toString());      
      } catch (IOException e) {
         System.out.println(e.toString());      
      }
   }
   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\xml>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:

  • The Document object is also a Node object, which is presented by the first line in the output.
  • The "xml" processing instruction is not part of the document object.
  • The second line in the output says that the root element is named as "body", of type 1, has 1 child node, has 0 attribute, and has no value.
  • The third line in the output says that there is child node nested inside the "body" node. The child node is called "#text", of type 3, has 0 child node, could not have any attribute, and has a value of string "Hello world!".
  • Note that the text enclosed by the "body" tags is parsed into a node separated from the "body" node.

Notes and sample codes in this section are based on JDK 1.4.1_01.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

DOM (Document Object Model) Programming Interface

 What Is DOM (Document Object Model)?

 Using DOM Implementation Provided in JDK 1.4

 DOM Specifications and DOM Node Interface

DOMBrowser.java - DOM Interface Java Example

 XML DOM Node Tree Example

 Building a New DOM Document Object

 Converting DOM Document Objects to XML Files

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating an XML Document against the Specified XML Schema

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
DOMBrowser.java - DOM Interface Java Example