xml解析时禁止网上下载dtd 规格严格

最近一直在研究xml解析问题,总结了一点小知识,就写下来吧!
dom解析时,会根据xml文件头的内容网上下载DTD文档,很烦人,速度慢不说,网络如果断了,程序也无法进行了。查了半天资料,终于知道如何解决了。以下为解决方案:

解决方案一:
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);//
解决方案二:
DocumentBuilder parser = builder.newDocumentBuilder();
EntityResolver resolver = new EntityResolver() {
public InputSource resolveEntity(String publicId,String systemId)
throws SAXException, IOException {
if (publicId.equals("-//Hibernate/Hibernate Configuration DTD//EN")) {
return new InputSource("../pstn_xml/hibernate-configuration-3.0.dtd");
}
return null;
}
};
parser.setEntityResolver(resolver);
原文地址:https://www.cnblogs.com/diyunpeng/p/2133857.html