hi,
I am using sax parser for validating xml file against xsd. The code looks like following;
package validate;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class Validator {
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-schemaLocation",
SchemaUrl);
ValidatorHandler handler = new ValidatorHandler();
parser.setErrorHandler(handler);
parser.setEntityResolver(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());
e.printStackTrace();
}
}
private class ValidatorHandler extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
System.out.println("Error :" +exception.getMessage());
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
System.out.println("FatalError :"+exception.getMessage());
}
public void warning(SAXParseException exception)
throws SAXException {
System.out.println("FatalError :"+exception.getMessage());
}
}
}
I am calling it like;
public class Main {
public static void main(String[] args) {
String SchemaUrl = "file://c://wf/wfschema.xsd";
String XmlDocumentUrl = "c://test.xml";
Validator validator = new Validator();
validator.validateSchema(SchemaUrl, XmlDocumentUrl);
}
But it throws exception like
FatalError :http://www.w3.org/TR/xml-schema-1#SchemaLocation?file://c://wf/wfschema.xsd
Error :cvc-elt.1: Cannot find the declaration of element 'process'.
XML Document has Error:truecvc-elt.1: Cannot find the declaration of element 'process'.
I think xsd file path is missing somewhere. Can any one please suggest how to overcome this.
Thank you.