Validating xml against xsd code:
private static final
String schemaSource = "schema.xsd";
private static final String JAXP_SCHEMA_SOURCE ="http://java.sun.com/xml/jaxp/properties/schemaSource";
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilderFactory.setValidating(validation);
try {
documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE,new File(schemaSource));
}
catch (IllegalArgumentException x) {
x.printStackTrace();
}
documentBuilder.setErrorHandler(new IMSParserErrorHandler());
Document document = documentBuilder.parse(configFile);
my schema is in : c:\data\schema.xsd..
i have the c:\data in my classpath.. so i gave the relative path as schema.xsd to my schema url.
error is :
Message: schema_reference.4: Failed to read schema document 'IMSSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
XML
<sample xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>
</sample>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</xsd:schema>