5

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?

chama
  • 5,973
  • 14
  • 61
  • 77

2 Answers2

2

I fought with this exact same problem for three hours and finally I found this:

xerces.apache.org/xerces2-j/features.html

In short, you need to add the following line:

spf.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);

  • 1
    This URL doesn't exist...maybe you could reference the official source: http://xerces.apache.org/xerces2-j/features.html – devyndraen Jan 30 '14 at 15:53
  • I have the same approach and setting the SaxParserFactory feature `http://apache.org/xml/features/xinclude/fixup-base-uris` to false did not help here. Anyone else who had success with this? – René Apr 13 '17 at 12:37
0

I had the exact same issue. Actually, the href attribute expects an URI, which can be:

  • Either an HTTP address (which means your included XML must be hosted somewhere)
  • Or a file on your local machine. But in that case, you need to prefix it with "file:..." and provide the absolute path.

With your example:

<?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="file:absolute-path-to-xml-files/included.xml"/>
</tag1>
rnxrnask
  • 1
  • 1