SaxXmlDtdValidation.java - SAX Parser with DTD Validation

Q

How to parse an XML file with DTD validation using the SAX API?

✍: FYIcenter

A

if you want to parse an XML file with DTD validation using the SAX API, you can follow these suggestions:

1. Set the validation flag to true on SAXParserFactory object:

      SAXParserFactory f = SAXParserFactory.newInstance();
	  f.setValidating(Boolean.parseBoolean(args[1]));
      SAXParser p = f.newSAXParser();

3. Impement validation event handler methods, warning(), Error(), fatalError():

   public void error(SAXParseException e) throws SAXException {
      System.out.println("\nError: "+e.toString()); 
   }

3. Provide the XML file with DTD included, UserError.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<!DOCTYPE User [
   <!ELEMENT User (ID+, BirthDate+, Name+, Sex+)>
   <!ELEMENT ID (#PCDATA)>
   <!ELEMENT BirthDate (#PCDATA)>
   <!ELEMENT Name (#PCDATA)>
   <!ELEMENT Sex (#PCDATA)>
]>

<User>
    <ID>101</ID>
    <Name>Frank Y. Ivy</Name>
</User>

Here is a complete example of parsing an XML with DTD validation using SAX API, SaxXmlDtdValidation.java:

// Copyright (c) 2017 FYIcenter.com
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.*;

public class SaxXmlDtdValidation extends DefaultHandler {
   static String dot = "............................................................";
   static int l = 0;
   public static void main(String[] args) throws Exception {
      SAXParserFactory f = SAXParserFactory.newInstance();
	  f.setValidating(Boolean.parseBoolean(args[1]));
      SAXParser p = f.newSAXParser();
      System.out.println("Parser class: "+p.getClass().getName());

	  p.parse(new File(args[0]), new SaxXmlDtdValidation());
   }
   public void startElement(String uri, String lName, String qName, Attributes atts) {
      l++;
      System.out.print("\n"+dot.substring(0,l)+lName+qName);
   }
   public void endElement(String uri, String lName, String qName) {
      l--;
   }
   public void characters(char[] ch, int start, int length) {
      System.out.print("("+(new String(ch,start,length))+")");
   }
   public void error(SAXParseException e) throws SAXException {
      System.out.println("\nError: "+e.toString()); 
   }
}

Compile and run the example program, SaxXmlDtdValidation.java:

>\fyicenter\jdk-1.8.0\bin\javac SaxXmlDtdValidation.java

>\fyicenter\jdk-1.8.0\bin\java SaxXmlDtdValidation UserError.xml true

Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl

.User
..ID(101)
..Name(Frank Y. Ivy)
Error: org.xml.sax.SAXParseException; systemId: file:/fyicenter/UserError.xml; 
lineNumber: 14; columnNumber: 8; The content of element type "User" must match 
"(ID+,BirthDate+,Name+,Sex+)".

If you run it again with "false", you will not see any validation errors:

>\fyicenter\jdk-1.8.0\bin\java SaxXmlDtdValidation UserError.xml false

Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl

.User
..ID(101)
..Name(Frank Y. Ivy)

 

Using XML SAX API with Apache Xerces

⇒⇒FAQ for Apache Xerces XML Parser

2017-12-09, 413👍, 0💬