// it's not all that good to have a concrete dependency on Xerces
// and a particular version, at that.
// but schema support in the Xerces series of parsers is variable
// and some of the configuration details differ.
// At least this way seems reliable
SAXParser parser = new SAXParser();
// Set features
parser.setFeature(NAMESPACES_FEATURE_ID, true);
parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
parser.setFeature(VALIDATION_FEATURE_ID, true);
parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
// Set properties
parser.setProperty(NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID, "schema.xsd");
XMLUnitHandler handler = new XMLUnitHandler(schemaSource);
// Set handlers
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.setEntityResolver(handler);
// parse document
parser.parse(documentSource);
handler.reportErrors();
}