Creating your own DOM error handler

Call the method setErrorHandler and pass in your custom class that implements org.xml.sax.ErrorHandler. Make sure you call the method setValidating(true)!

The following example prints out a bunch of error messages, because the XML doesn’t match the DTD.

customers.dtd (!!remove the space between ? and xml):

<? xml version="1.0" encoding="UTF-8"?>
<!ELEMENT customer (name, addresses+, email)+ >
<!ATTLIST customer custid CDATA #REQUIRED>
 
<!ELEMENT name (#PCDATA)>
 
<!ELEMENT addresses (addline1, addline2, zip, location, state)>
<!ELEMENT addline1 (#PCDATA)>
<!ELEMENT addline2 (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT state (#PCDATA)>
 
<!ELEMENT email (#PCDATA)>

customers.xml (!!remove the space between ? and xml):

<? xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customers SYSTEM "customers.dtd">
<customers>
   <customer id="C12345">
      <name>Joris Van den Bogaert</name>
      <address>
         <addressline>Handelskaai 3</addressline>
         <zip>1000</zip>
         <location>Brussels</location>
         <country>BELGIUM</country>
      </address>
   </customer>
   <destination>
      <address>123.321.1.20</address>
   </destination>
</customers>

Main.java:

import org.w3c.dom.*;
  
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
 
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
 
import java.io.*;
   
public class Main
{
   public static void main(String []args) {
      Document doc;
  
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setValidating(true);
         
         DocumentBuilder db = dbf.newDocumentBuilder();
         MyErrorHandler handler = new MyErrorHandler();
         db.setErrorHandler(handler);
         doc = db.parse(new File("customers.xml")); 
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   } 
}
 
class MyErrorHandler implements ErrorHandler {
   public void warning(SAXParseException e) throws SAXException {
      System.err.println("[warning] "+e.getMessage());
   }
 
   public void error(SAXParseException e) throws SAXException {
      System.err.println("[error] "+e.getMessage());
   }
 
   public void fatalError(SAXParseException e) throws SAXException {
      System.err.println("[fatal error] "+e.getMessage());
      throw e;
   }
};

outputs:

[error] Element type "customers" must be declared.
[error] Attribute "custid" is required and must be specified for element type "customer".
[error] Attribute "id" must be declared for element type "customer".
[error] Element type "address" must be declared.
[error] Element type "addressline" must be declared.
[error] Element type "country" must be declared.
[error] The content of element type "customer" must match "(name,addresses+,email)+".
[error] Element type "destination" must be declared.
[error] Element type "address" must be declared.