My input is a well-formed XML document and a corresponding XML Schema document. What I would like to do is determine the location within the XML document that causes it to fail validation against the XML Schema document. I could not figure out how to do this using the standard validation approach in Java:
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(... /* the .xsd source */);
Validator validator = schema.newValidator();
DocumentBuilderFactory ...
DocumentBuilder ...
Document document = DocumentBuilder.parse(... /* the .xml source */);
try {
validator.validate(new DOMSource(document));
...
} catch (SAXParseException e) {
...
}
I have toyed with the idea of getting at least the line and column number from SAXParseException, but they're always set to -1, -1 on validation error.
A DOM does not retain information about its source -- in most cases it's irrelevant, and DOM is meant to be manipulated (ie, any location information would be incorrect).
The solution is to validate at the time you parse: call DocumentBuilderFactory.setSchema() before creating the DocumentBuilder
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With