xml解析时禁止网上下载dtd

来源:互联网 发布:centos挂载u盘 fdisk 编辑:程序博客网 时间:2024/05/22 00:35
最近一直在研究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);
原创粉丝点击