http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html
Validating a XML document capturing ALL the validation errors is a bit roundabouthish:
first you must create a ErrorHandler:
public static class XMLValidationErrorHandler implements ErrorHandler { Listerrors = new ArrayList (); public void warning(SAXParseException ex) { errors.add(new ValidationError(ex.getLocalizedMessage())); logger.error("ErrorHandler warning " + ex.getLocalizedMessage()); } public void error(SAXParseException ex) { errors.add(new ValidationError(ex.getLocalizedMessage())); logger.error("ErrorHandler error " + ex.getLocalizedMessage()); } public void fatalError(SAXParseException ex) { errors.add(new ValidationError(ex.getLocalizedMessage())); logger.error("ErrorHandler fatalError " + ex.getLocalizedMessage()); } public List getErrors() { return errors; } }
and then assign it to the unmarshaller:
Listerrors = new ArrayList (); byte[] bytes = xmlAsString.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); Source paramSource = new StreamSource(bais); Schema schema = unmarshaller.getSchema(); Validator validator = schema.newValidator(); XMLValidationErrorHandler errorHandler = new XMLValidationErrorHandler(); validator.setErrorHandler(errorHandler); validator.validate(paramSource); errors = errorHandler.getErrors();
I really feel that things should be easier than that...