DOMValidator.java - Validating XML with DTD using DOM

This section provides a tutorial example on how to use DOM API provided in JDK 1.4 to write a Java program to validate an XML document against the specified DTD document type.

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

To test DOMValidator, I created the following XML file with DTD statements, invalid_dtd.xml:

<?xml version="1.0"?>
<!-- invalid_dtd.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<!DOCTYPE dictionary [
 <!ELEMENT dictionary (note, word+)>
 <!ELEMENT note ANY>
 <!ELEMENT word (update?, name, definition+, usage*)>
 <!ELEMENT update EMPTY>
 <!ATTLIST update 
  date CDATA #REQUIRED 
  editor CDATA #IMPLIED
 >
 <!ELEMENT name (#PCDATA)>
 <!ATTLIST name is_acronym (true | false) "false">
 <!ELEMENT definition (#PCDATA)>
 <!ELEMENT usage (#PCDATA | i)*>
 <!ELEMENT i (#PCDATA)>
 <!ENTITY herong "Dr. Herong Yang">
]>
<dictionary>
 <note>Copyright (c) 2014 by &herong;</note>
 <word>
  <name is_acronym="true" language="EN">POP</name>
  <definition>Post Office Protocol</definition>
  <definition>Point Of Purchase</definition>
 </word>
 <word>
  <update date="2014-12-23"/> 
  <name is_acronym="yes">XML</name>
  <definition>eXtensible Markup Language</definition>
  <note>XML comes from SGML</note>
 </word>
 <word>
  <update editor="Herong Yang"/> 
  <name>markup</name>
  <definition>The amount added to the cost price to calculate 
the selling price - <i>Webster</i></definition>
 </word>
</dictionary>

Run the sample Java program, "java DOMValidator invalid_dtd.xml" in JDK 1.8, I got:

Error:
   Public ID: null
   System ID: file:/C:/herong/xml/invalid_dtd.xml
   Line number: 24
   Column number: 41
   Message: Attribute "language" must be declared for element type 
      "name".
Error:
   Public ID: null
   System ID: file:/C:/herong/xml/invalid_dtd.xml
   Line number: 30
   Column number: 26
   Message: Attribute "is_acronym" with value "yes" must have a value
      from the list "true false ".
Error:
   Public ID: null
   System ID: file:/C:/herong/xml/invalid_dtd.xml
   Line number: 33
   Column number: 9
   Message: The content of element type "word" must match 
      "(update?,name,definition+,usage*)".
Error:
   Public ID: null
   System ID: file:/C:/herong/xml/invalid_dtd.xml
   Line number: 35
   Column number: 33
   Message: Attribute "date" is required and must be specified for 
      element type "update".
Error:
   Public ID: null
   System ID: file:/C:/herong/xml/invalid_dtd.xml
   Line number: 38
   Column number: 48
   Message: The content of element type "definition" must match "null"

This is perfect. It tells you where the error is, and why it's an error.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 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

DOMValidator.java - Validating XML with DTD using DOM

 SAXValidator.java - Validating XML with DTD using SAX

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 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++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version