您需要EnitityResolver2
在您的上设置一个自定义,DocumentBuilder
以便您可以在处理InputSource
时返回正确xi:include
的。
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;
}
});
这现在将返回类加载器加载的资源的一个InputSource
。InputStream
您可能需要操纵String
以获得正确的路径。