4 Replies Latest reply on May 21, 2019 7:59 AM by mganserer

    XML schema file cannot be loaded

    mganserer

      Hello,

       

      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

        • 1. Re: XML schema file cannot be loaded
          zhurlik

          Hi,

           

          Could you execute `ls -l <your war file>` and `unzip -l <your war file> | grep xsd` and provide the results

           

          Thanks,

          Vlad

          • 2. Re: XML schema file cannot be loaded
            lafr

            /content/bugtrack.war/WEB-INF/classes/schema/theschema.xsd is not the place where the file is really on your system. It's a relative path within the JBOSS_HOME server location.

            When accessing files within my deployment unit I usually use constructs like this one: InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( "META-INF/" + href );

             

            • 3. Re: XML schema file cannot be loaded
              jaikiran

              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);

              The "java.io.File" API that you are using isn't best suited for this usage. Instead you can change the code to use the "java.net.URL" based API instead. Something like:

               

              SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

                Schema schema = sf.newSchema(this.getClass().getClassLoader().getResource("schema/theschema.xsd"));

                unmarshaller.setSchema(schema);

              • 4. Re: XML schema file cannot be loaded
                mganserer

                Hi,

                using your approach with the 'java.net.URL' did the job! Thank you very much!