|
DTD Validation in Java
Part:
1
2
(Continued from previous part...)
After entering, java DOMValidator invalid_dtd.xml, I got:
Error:
Public ID: null
System ID: file:C:/herong/invalid_dtd.xml
Line number: 24
Column number: -1
Message: Attribute "language" is not declared for element "name".
Error:
Public ID: null
System ID: file:C:/herong/invalid_dtd.xml
Line number: 30
Column number: -1
Message: Value "yes" is not one of the enumerated values
for this attribute.
Error:
Public ID: null
System ID: file:C:/herong/invalid_dtd.xml
Line number: 32
Column number: -1
Message: Element "word" does not allow "note" here.
Error:
Public ID: null
System ID: file:C:/herong/invalid_dtd.xml
Line number: 35
Column number: -1
Message: Attribute value for "date" is #REQUIRED.
Error:
Public ID: null
System ID: file:C:/herong/invalid_dtd.xml
Line number: 38
Column number: -1
Message: Element "definition" does not allow "i" -- (#PCDATA)
This is perfect. It tells you where the error is, and why it's an error.
Validation with SAX
Actually, SAX and DOM are sharing the same XML validation implementation.
In this implementation, error events are fired during the parsing process.
Application programs that are interested in validation errors must
implement the error event handlers defined in the org.xml.sax.ErrorHandler
interface.
In the SAX design, the ErrorHandler interface is integrated into the
DefaultHandler class. Application programs just need to extend DefaultHandler
and override those error event handlers.
Here is my SAX based XML validation program, SAXValidator.java:
/**
* SAXValidator.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
class SAXValidator {
public static void main(String[] args) {
try {
File x = new File(args[0]);
SAXParserFactory f = SAXParserFactory.newInstance();
f.setValidating(true); // Default is false
SAXParser p = f.newSAXParser();
DefaultHandler h = new MyErrorHandler();
p.parse(x,h);
} 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 extends DefaultHandler {
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());
}
}
}
Note that:
- SAXParserFactory.setValidating(true) is called to turn on the validation funtion.
- The structure of this program is 99% identical to SAXBrowser.java. Since I am only
interested the error events, only the error event handlers are overrode. If I
copy the contend event handlers here, and this program will be working as a browser
and a validator at the same time.
Run java SAXValidator invalid_dtd.xml, you will get the same output as DOMValidator.
Part:
1
2
|