- Parsing XML Files with DOM

This section provides a tutorial example on how to write an XML file parser,, 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.

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

 - Copyright (c) 2014,, All Rights Reserved.
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();
         DocumentBuilder b = f.newDocumentBuilder();
         Document d = b.parse(x);
         DOMImplementation i = d.getImplementation();
      } catch (ParserConfigurationException e) {
      } catch (SAXException e) {
      } catch (IOException e) {

Now let's use this program to parse my first XML file, hello.xml:

<?xml version="1.0"?>
<body>Hello world!</body>

Output with JDK 1.8:

>java DOMParser hello.xml

[#document: null]

Note that:

If you are using JDK 1.4.1, the output should look like:

>java DOMParser hello.xml


Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 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 - Parsing XML Files with DOM - Browsing DOM Tree Structure - Building a New DOM Document - 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)

 Outdated Tutorials


 PDF Printing Version