I am trying to create a combined xml document using XInclude to be unmarshalled via JAXB.
Here is my unmarshalling code:
@Override
public T readFromReader(final Reader reader) throws Exception {
final Unmarshaller unmarshaller = createUnmarshaller();
final SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setXIncludeAware(true);
spf.setNamespaceAware(true);
//spf.setValidating(true);
final XMLReader xr = spf.newSAXParser().getXMLReader();
final SAXSource source = new SAXSource( xr, new InputSource(reader) );
try {
final T object = (T) unmarshaller.unmarshal(source);
postReadSetup(object);
return object;
} catch (final Exception e) {
throw new RuntimeException("Cannot parse XML: Additional information is attached. Please ensure your XML is valid.", e);
}
}
Here is my main xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<tag1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:schemaLocation="path-to-schema/schema.xsd">
<xi:include href="path-to-xml-files/included.xml"></xi:include>
</tag1>
And included.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tag2> Some text </tag2>
In order to actually unmarshal it, I create a new FileReader
with the path to my xml file (path-to-xml-files/main.xml - the path is correct because it can clearly find the main file). When I run it, however, there is something wrong with the included file. I am getting an UnmarshalException with a linked SAXParseException with this error message: Error attempting to parse XML file (href='path-to-xml-files/included.xml').
When I manually merge the content of included.xml into main.xml, it runs with no problems.
I can't tell if it's a JAXB issue or an XInclude issue, though I strongly suspect the latter.
What am I missing?