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) 2013, HerongYang.com, 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:

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

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

Java API for XML Processing - JAXP

 What Is JAXP?

 Downloading and installing Java SE 1.6 Update 2

 Compiling and Running Java Programs

XML File DOM Parser - XmlDomFileParser.java

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version