Java XML validation does not work when schema comes from classpath

HunterMystic

New Member
I am validating XML documents against a schema. Some more complex documents/schemas always fail when trying to validate them using this code:\[code\] DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); dbfac.setNamespaceAware(true); dbfac.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.parse("sampleResponse.xml"); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaSource = new StreamSource(getClass().getResourceAsStream("/" + "SampleResponse.xsd")); Schema schema = schemaFactory.newSchema(schemaSource); Validator validator = schema.newValidator(); Source source = new DOMSource(doc); // Set a custom error handler that simple re-throws every exception validator.setErrorHandler(new ValidationErrorHandler()); validator.validate(source);\[/code\]The problem is this line:\[code\] Source schemaSource = new StreamSource(getClass().getResourceAsStream("/" + "SampleResponse.xsd"));\[/code\]If I read the schema as a file, it works:\[code\] Source schemaSource = new StreamSource(new File("somepath/SampleResponse.xsd"));\[/code\]Why doesn't validation work when I get the schema directly from classpath?(Using Java 1.6 on Windows 7 64-bit)Exception message when failing:\[code\]Could not validate against schema SampleResponse.xsd. Nested exception: src-resolve: Cannot resolve the name 'oa:Attachments' to a(n) 'element declaration' component.\[/code\]
 
Back
Top