最近一直在研究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);

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2022-12-23
  • 2022-01-03
  • 2021-08-08
  • 2021-03-31
  • 2022-12-23
猜你喜欢
  • 2021-05-31
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案