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

XML File DOM Parser - XmlDomFileParser.java

This section describes a tutorial example on how to create an XML file DOM parser using JAXP DocumentBuilder and DocumentBuilderFactory classes.

The most common use of JAXP is the parser interface and the DOM representation of XML documents. If you want read XML a file and manipulate its contents as DOM object tree, you need create a DOM parser instance, and use it to parse the XML file into a DOM Document object.

Here is simple tutorial example code called XmlDomFileParser.java that parses an XML file and create a Document instance:

/*
 * XmlDomFileParser.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.File;
class XmlDomFileParser {
  public static void main(String[] a) {
    if (a.length<1) {
      System.out.println("Usage:");
      System.out.println("java XmlDomFileParser xml_file_name");
    } else {
      String name = a[0];

      // parsing the XML file
      Document document = parseXmlDom(name);
      System.out.println();
      System.out.println("Root Node Name: "
        + document.getChildNodes().item(0).getNodeName());
    }
  }
  public static Document parseXmlDom(String name) {
    Document document = null;
    try {

      // getting the default implementation of DOM builder
      DocumentBuilderFactory factory 
         = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      System.out.println();
      System.out.println("Factory Class: "
        + factory.getClass().getName());
      System.out.println("Builder Class: "
        + builder.getClass().getName());

      // parsing the XML file
      document = builder.parse(new File(name));
      System.out.println();
      System.out.println("Document Class: "
        + document.getClass().getName());
      System.out.println("DOMImplement Class: "
        + document.getImplementation().getClass().getName());

    } catch (Exception e) {
      // catching all exceptions
      System.out.println();
      System.out.println(e.toString());
    }
    return document;
  }
}

As the first test, let's try to parse the following simple XML file, first_html.xml:

<?xml version = "1.0" encoding = "utf-8"?>
<html>
<body>My first HTML document in XML format.</body>
</html>

Here is what you will get as output:

>javac XmlDomFileParser.java

>java XmlDomFileParser first_html.xml

Factory Class: com.sun.org.apache.xerces.internal.jaxp
   .DocumentBuilderFactoryImpl
Builder Class: com.sun.org.apache.xerces.internal.jaxp
   .DocumentBuilderImpl

Document Class: com.sun.org.apache.xerces.internal.dom
   .DeferredDocumentImpl
DOMImplement Class: com.sun.org.apache.xerces.internal.dom
   .DeferredDOMImplementationImpl

Root Node Name: html

The output shows that:

  • The default implemenation classes of the DocumentBuilderFactory, DocumentBuilderFactory, Document, and DOMImplementation classes are all from the Apache Xerces project.
  • The root element name "html" shows that XML file was parsed correctly.

If you read the tutorial examples recorded in my other book "JDK Tutorials - Herong's Tutorial Notes", you will see that JDK 1.4 was using a different set of default implementation classes from the Apache Crimson project:

org.apache.crimson.jaxp.DocumentBuilderFactoryImpl
org.apache.crimson.jaxp.DocumentBuilderImpl
org.apache.crimson.tree.XmlDocument
org.apache.crimson.tree.DOMImplementationImpl

Sections in This Chapter

What Is JAXP?

Downloading and installing Java SE 1.6 Update 2

Compiling and Running Java Programs

XML File DOM Parser - XmlDomFileParser.java

Dr. Herong Yang, updated in 2007
XML File DOM Parser - XmlDomFileParser.java