users@jaxb.java.net

Re: Reading .XSD and .XML Files from Jars

From: Joshua Smith <josh_at_rationalpi.com>
Date: Fri, 7 Sep 2007 11:24:41 -0400

Thank you Bhakti and Kenny. That worked perfectly. Below is the final code.

Josh

***begin code***
// unmarshall objects
InputStream schemaStream = Main.class.getResourceAsStream("/outlet.xsd");
InputStream xmlStream = Main.class.getResourceAsStream("/xmloutlet.xml");
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 StreamSource(schemaStream));
  Unmarshaller unmarshaller = context.createUnmarshaller();
  unmarshaller.setSchema(schema);
  unmarshalledObject = unmarshaller.unmarshal(xmlStream);
} 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);
}
***end code***

On 9/7/07, Kenny MacLeod <kennym_at_kizoom.com> wrote:
>
> To clarify, you need to load the files as classpath resources, rather
> than as files, for example:
>
> InputStream stream =
>
> getClass().getResourceAsStream("ic/plugin/pulizzi/outletcontrol/outlet.xsd");
>
> Schema schema = SchemaFactory.newInstance(stream);
>
>
> Joshua Smith wrote:
> > Thank you. I'll give that a try.
> >
> > Josh
> >
> > On 9/6/07, *Bhakti Mehta* <Bhakti.Mehta_at_sun.com
> > <mailto:Bhakti.Mehta_at_sun.com>> wrote:
> >
> > 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
> > <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***
> > >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> > <mailto:users-unsubscribe_at_jaxb.dev.java.net>
> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> > <mailto:users-help_at_jaxb.dev.java.net>
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>