Programmatically determining which node in an XML document caused a validation against its XML schema

My input is a well-formed XML document and the corresponding XML Schema document. What I would like to do is determine the location in the XML document, which causes it to refuse to validate against the XML Schema document. I could not figure out how to do this using the standard validation method 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 played with the idea of ​​getting at least the row and column number from SAXParseException, but they are always set to -1, -1 with a validation error.

+5
source share
3 answers

DOM - , DOM (.. ).

: DocumentBuilderFactory.setSchema() DocumentBuilder.

+1

, . , , ..

try {
   validator.setErrorHandler(handler);
   validator.validate(...);
} catch (SAXParseException e) {
   // Use handler info, or log it in handler
}

, : ErrorHandler

0
0

All Articles