DOMValidator.java - XML DTD Validation with DOM

This section provides a tutorial example on how to write a DTD validator using DOM classes in JDK. The validator can validate an XML file against the specified DTD statements.

JDK offers a document builder interface, javax.xml.parsers.DocumentBuilder, to represent classes that can parse XML files into document objects described in the Document Object Model (DOM). See my notes on DOM for more details.

The document builder also supports DTD validation during the parsing process. To do this:

Here is a sample program, DOMValidator.java, that shows you how to use a document builder class to validate XML files against the specified DTD statements.

/* DOMValidator.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.helpers.DefaultHandler;
class DOMValidator {
   public static void main(String[] args) {
      try {
      	 File x = new File(args[0]);
         DocumentBuilderFactory f 
            = DocumentBuilderFactory.newInstance();
         f.setValidating(true); // Default is false
         DocumentBuilder b = f.newDocumentBuilder();
         // ErrorHandler h = new DefaultHandler();
         ErrorHandler h = new MyErrorHandler();
         b.setErrorHandler(h);
         Document d = b.parse(x);
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString()); 	
      } catch (SAXException e) {
         System.out.println(e.toString()); 	
      } catch (IOException e) {
         System.out.println(e.toString()); 	
      }
   }
   private static class MyErrorHandler implements ErrorHandler {
      public void warning(SAXParseException e) throws SAXException {
         System.out.println("Warning: "); 
         printInfo(e);
      }
      public void error(SAXParseException e) throws SAXException {
         System.out.println("Error: "); 
         printInfo(e);
      }
      public void fatalError(SAXParseException e) throws SAXException {
         System.out.println("Fattal error: "); 
         printInfo(e);
      }
      private void printInfo(SAXParseException e) {
         System.out.println("   Public ID: "+e.getPublicId());
         System.out.println("   System ID: "+e.getSystemId());
         System.out.println("   Line number: "+e.getLineNumber());
         System.out.println("   Column number: "+e.getColumnNumber());
         System.out.println("   Message: "+e.getMessage());
      }
   }
}

See the next section on how to test this DTD validator.

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

 SAX (Simple API for XML)

DTD (Document Type Definition) - XML Validation

DOMValidator.java - XML DTD Validation with DOM

 Testing DOM XML DTD Validator

 SAXValidator.java - XML DTD Validation with SAX

 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

 References

 PDF Printing Version