SAXBuilder builder=new SAXBuilder();
builder.setEntityResolver(new YourDTDResolver());
builder.setValidation(true);
YourDTDResolver implements EntityResolver,实现接口定义的
public abstract InputSource resolveEntity (String publicId,String systemId)throws SAXException, IOException;
方法,在其中添加可以从本地加载dtd的方法,如:
public InputSource resolveEntity(String publicId, String systemId) {
if (systemId != null && systemId.startsWith(URL)) {
logger.debug("trying to locate " + systemId + " in classpath under "+CONFIG_PATH);
// Search for DTD
String path = CONFIG_PATH + systemId.substring(URL.length());
InputStream dtdStream = resourceLoader == null ? getClass()
.getResourceAsStream(path) : resourceLoader
.getResourceAsStream(path);
if (dtdStream == null) {
logger.debug(systemId + " not found in classpath");
return null;
} else {
logger.debug("found " + systemId + " in classpath");
InputSource source = new InputSource(dtdStream);
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
}
} else {
// use the default behaviour
return null;
}
}
我这样讲你明白吗?