设置dom4j不检查xml中的dtd声明

今天在做一个web应用部署验证功能。部署的时候需要通过解析其配置文件来得到具体的contextRoot,以此来判断是否和服务器上以存在的应用contextRoot冲突。解析的配置文件的时候,xml中有dtd声明,dom4j读取xml的时候应该是去联网获取dtd了,相应的dtd只在本地存在,所以按照dtd的url会返回404,造成读取xml失败

Nested exception: java.io.FileNotFoundException: http://xxxxx/j2ee/dtds/xxxx.dtd
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1194)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:973)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:905)

后来google了一下,这篇文章的方式可以解决
[url]http://blog.csdn.net/luo_yifan/article/details/5904369[/url]
主要是设置一下SAXReader的EntityResolver,之后就不会再检查dtd了。 :idea:

SAXReader saxReader = new SAXReader();
saxReader.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
InputSource is = new InputSource(new StringBufferInputStream(""));
is.setPublicId(publicId);
is.setSystemId(systemId);
return is;
}
});

需要注意的是:
InputSource is = new InputSource(new StringBufferInputStream("")); 
这中间的StringBufferInputStream如果换成了自己传过来的inputstream,对于zip文件可能正常解析,对于FileInputStream,直接指向某个xml文件时,会出现IOException:Read error 原因还不清楚 :oops:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值