Programatically determining which node in an XML document caused validation against its XML Schema t

Posted by jd1212 on Stack Overflow See other posts from Stack Overflow or by jd1212
Published on 2010-06-07T16:48:35Z Indexed on 2010/06/07 16:52 UTC
Read the original article Hit count: 888

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml