users@jaxb.java.net

Re: Reading .XSD and .XML Files from Jars

From: Bhakti Mehta <Bhakti.Mehta_at_Sun.COM>
Date: Thu, 06 Sep 2007 15:01:42 -0700

You can do a getResourceAsStream and create a new Schema with a Source
which could process this input stream

Regards,
Bhakti

Joshua Smith wrote:
> All-
>
> I am trying to use JAXB with a schema and an XML file that are
> contained within my Jar file. The code below works fine if the .xsd
> and .xml file are in a directory that is included in the classpath,
> but fails if the files are bundled in the Jar file. I've confirmed
> that the files do exist in the Jar and that their contents are valid.
>
> Does anyone know if there are any problems with JAXB reading schemas
> and XML file from Jar files?
> The error message and my code are below. Of particular note, is the
> path in the error message. It looks like two file URLs concatenated
> together.
>
> Thanks,
> Joshua Smith
>
> ***begin error message***
> Exception: org.xml.sax.SAXParseException: schema_reference.4: Failed
> to read schema document
> 'file:/C:/JavaProjects2/demarshallingFromJar/file:/C:/JavaProjects2/demarshallingFromJar/demarshallingFromJar.jar!/outlet.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>.
> org.xml.sax.SAXParseException: schema_reference.4: Failed to read
> schema document
> 'file:/C:/JavaProjects2/demarshallingFromJar/file:/C:/JavaProjects2/demarshallingFromJar/demarshallingFromJar.jar!/outlet.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>.
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError
> (Unknown Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema
> (Unknown Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar
> (Unknown Source)
> at
> com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(Unknown
> Source)
> at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
> at javax.xml.validation.SchemaFactory.newSchema (Unknown Source)
> at ic.plugin.pulizzi.outletcontrol.server.Main.main(Main.java:30)
> ***end error message***
>
> ***begin source***
> // unmarshall objects
> String schemaFilename = Main.class.getResource("/outlet.xsd").getPath();
> String xmlFilename = Main.class.getResource("/xmloutlet.xml").getPath();
> List<OutletIF> currentConfiguration = null;
>
> Object unmarshalledObject = null;
>
> try {
> JAXBContext context = JAXBContext.newInstance(
> ic.plugin.pulizzi.outletcontrol.outlet.OutletInfo.class );
> final SchemaFactory sf = SchemaFactory.newInstance(
> "http://www.w3.org/2001/XMLSchema ");
>
> final Schema schema = sf.newSchema(new File(schemaFilename));
> Unmarshaller unmarshaller = context.createUnmarshaller();
> unmarshaller.setSchema(schema);
> unmarshalledObject = unmarshaller.unmarshal(new File(xmlFilename));
> } catch (Exception ex) {
> System.out.println ("Exception: " + ex);
> ex.printStackTrace();
> System.exit(-1);
> }
>
> // check if demarshalled object was null
>
> if (unmarshalledObject == null) {
>
> // null result
> System.out.println ("Demarshalling failure. Object was null.");
> System.exit(-1);
> }
>
> // all good, print out demarshalled objects
> final OutletInfo outletInfo = (OutletInfo)unmarshalledObject;
> currentConfiguration = outletInfo.getOutlets();
> for (OutletIF outlet : currentConfiguration) {
> System.out.println(outlet);
> }
>
> System.exit(0);
> ***end source***
>