Q:

File for xi:include does not exist because parser is looking from project root

I currently have an xml resource file that has XML inclusions.
stream = Main.class.getResourceAsStream("resource/Resource.xml");
within the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude">   
    <element />
    <xi:include href="resource/1.xml"/>
</semantics>

However, after parsing Resource.xml, I get an error that the file being included does not exist. 
After checking, it seems that the path was concatenated with the root directory of my project, however my problem is that the resource file 1.xml will be inside a jar file later on.
Is it possible to make the DocumentBuilder to load inclusion as a reasource as well?

A:

You need to set a custom EnitityResolver2 on your DocumentBuilder so that you can return the correct InputSource when then xi:include is processed.
final DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
documentBuilder.setEntityResolver(new EntityResolver2() {
    @Override
    public InputSource getExternalSubset(String string, String string1) throws SAXException, IOException {
        return null;
    }

    @Override
    public InputSource resolveEntity(String string, String string1, String string2, String string3) throws SAXException, IOException {
        final String resourceName = string3;
        final InputSource is = new InputSource();
        is.setSystemId(resourceName);
        is.setByteStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName));
        return is;
    }

    @Override
    public InputSource resolveEntity(String string, String string1) throws SAXException, IOException {
        return null;
    }
});

This will now return an InputSource of the InputStream of the resource as loaded by the classloader. You may need to manipulate the String to get the right path.

