import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
public class SchemaValidator {
public void validateSchema(String SchemaUrl, String XmlDocumentUrl) {
SAXParser parser = new SAXParser();
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
SchemaUrl);
Validator handler = new Validator();
parser.setErrorHandler(handler);
parser.parse(XmlDocumentUrl);
if (handler.validationError == true) {
System.out.println("XML Document has Error:" +
handler.validationError + "" +
handler.saxParseException.getMessage());
} else {
System.out.println("XML Document is valid");
}
} catch (java.io.IOException ioe) {
System.out.println("IOException" + ioe.getMessage());
} catch (SAXException e) {
System.out.println("SAXException" + e.getMessage());
}
}
public static void main(String[] argv) {
String SchemaUrl = argv[0];
String XmlDocumentUrl = argv[1];
SchemaValidator validator = new SchemaValidator();
validator.validateSchema(SchemaUrl, XmlDocumentUrl);
}
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception)
throws SAXException {
}
}
}