I am unmarshalling an XML file into a JAXB-generated Java object. I would like the unmarshaller to validate the file against the schema in the process. The schema's .xsd
file is inside a dependencies .jar
file. I set the schema like so:
ClassLoader classLoader = getClass().getClassLoader();
InputStream schemaStream = classLoader.getResourceAsStream(schemaFilePath);
StreamSource schemaSource = new StreamSource(schemaStream);
Schema schema = factory.newSchema(schemaSource);
unmarshaller.setSchema(schema);
That mostly works except for one issue. The schema has some complex types factored out into other .xsd
files. The unmarshaller doesn't appear to be able to find the factored-out .xsd
files because when I attempt to set the schema I'm getting the SAXException
:
Cannot resolve the name 'tns:FactoredOutType' to a(n) 'type definition' component.
Note: This works fine when running from eclipse, when it references the .xsd
files from the target
folder instead of from the .jar
file.
Anybody have any ideas how I can get the factored-out .xsd
files working for a schema that is in a dependency's .jar
file?
Edit:
In case it's helpful information, the top-level .xsd
is in a model
folder, and the type it references is in model/common
, so I reference the top-level .xsd
as:
"model/TopLevel.xsd"
... and inside it, it references the factored-out .xsd
as:
"common/FactoredOut.xsd"