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

Using DOM Implementation Provided in JDK 1.4

This section provides a tutorial example on how to use the DOM interface included in JDK 1.4, which actually uses DOM implementation provided in the org.apache.crimson package.

Here is a program to show how different packages are used together to parse an XML file into a DOM document object:

/**
 * DOMParser.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 DOMParser {
   public static void main(String[] args) {
      try {
         File x = new File(args[0]);
         DocumentBuilderFactory f 
            = DocumentBuilderFactory.newInstance();
         System.out.println(f.toString());      
         DocumentBuilder b = f.newDocumentBuilder();
         System.out.println(b.toString());      
         Document d = b.parse(x);
         System.out.println(d.toString());      
         DOMImplementation i = d.getImplementation();
         System.out.println(i.toString());
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString());      
      } catch (SAXException e) {
         System.out.println(e.toString());      
      } catch (IOException e) {
         System.out.println(e.toString());      
      }
   }
}

If you run this sample program, you will get:

C:\herong\xml>java DOMParser hello.xml

org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@1c78e57
org.apache.crimson.jaxp.DocumentBuilderImpl@13e8d89
org.apache.crimson.tree.XmlDocument@1cfb549
org.apache.crimson.tree.DOMImplementationImpl@1820dda

Note that:

  • javax.xml.parsers.DocumentBuilderFactory.newInstance() method is used to create a new factory instance using a factory implementation from the org.apache.crimson.jaxp.* package.
  • javax.xml.parsers.DocumentBuilder.newDocumentBuilder() method is used to create a new builder instance using a builder implementation from org.apache.crimson.jaxp.* package.
  • javax.xml.parsers.DocumentBuilder.parse() method is used to parse the XML file into an org.w3c.dom.Document object implemented with org.apache.crimson.tree.XmlDocument class.

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

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2013
Using DOM Implementation Provided in JDK 1.4