XML schema file cannot be loaded
mganserer May 17, 2019 5:09 AMHello,
I am facing a problem with our application that runs on Wildfly 16. A small part of this system is responsible for importing data in XML format from external applications. We want to validate the incoming content by using a XML schema file that is contained in the WEB-INF folder of our Java EE 8 application.
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(this.getClass().getClassLoader().getResource("schema/theschema.xsd").getPath()));
unmarshaller.setSchema(schema);
When testing the code an exception will be thrown:
08:46:03,827 INFO [stdout] (default task-1) org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/content/app.war/WEB-INF/classes/schema/theschema.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>.
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:201) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:132) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:394) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:304) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4132) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument1(XSDHandler.java:2471) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:2180) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:558) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:580) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:547) ~[?:?]
08:46:03,827 INFO [stdout] (default task-1) at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:513) ~[?:?]
08:46:03,828 INFO [stdout] (default task-1) at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:234) ~[?:?]
08:46:03,828 INFO [stdout] (default task-1) at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:669) ~[?:?]
08:46:03,828 INFO [stdout] (default task-1) at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:685) ~[?:?]
08:46:03,828 INFO [stdout] (default task-1) at __redirected.__SchemaFactory.newSchema(__SchemaFactory.java:119) ~[jboss-modules.jar:1.9.0.Final]
...
08:46:03,836 INFO [stdout] (default task-1) Caused by: java.io.FileNotFoundException: /content/bugtrack.war/WEB-INF/classes/schema/theschema.xsd (No such file or directory)
...
I am sure that the location of the file is valid as I am able to read the content from the file manually. Just the method newSchema() seems not being able to read it!
Does somebody have an idea? Thank you in advance!
Best regards
Martin