JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

DOMParser.java - Parsing XML Files with DOM

This section provides a tutorial example on how to write an XML file parser, DOMParser.java, with the org.w3c.dom.Document class included in JDK.

What is DOM (Document Object Model)? DOM is an Application Programming Interface (API) that represents an XML file as a document object, which allows application programs to manage the information contained in the document object.

DOM has been implemented in Java in J2SDK 1.4.1_01, which is already installed on my system. So I am ready to play with XML files through DOM in Java.

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
 */
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()); 	
      }
   }
}

Output:

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 fatory 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.

Last update: 2006.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.3.1 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 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

 Socket Network Communication

 Datagram Network Communication

DOM (Document Object Model) - API for XML Files

DOMParser.java - Parsing XML Files with DOM

 DOMBrowser.java - Browsing DOM Tree Structure

 DOMNewDoc.java - Building a New DOM Document

 DOMToXML.java - 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 - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
DOMParser.java - Parsing XML Files with DOM