dom4j解析xml-取消doctype中DTD验证设置
在编写⽤dom4j解析iBatis的sqlmap⽂件时,抛出以下异常信息:
org.dom4j.DocumentException: Nested exception:
at org.dom4j.ad(SAXReader.java:484)
at org.dom4j.ad(SAXReader.java:264)
...
Nested exception:
java.UnknownHostException:
at t(Unknown Source)
at t(Unknown Source)
at t(Unknown Source)
at sun.NetworkClient.doConnect(Unknown Source)
at sun.www.http.HttpClient.openServer(Unknown Source)
at sun.www.http.HttpClient.openServer(Unknown Source)
at sun.www.http.HttpClient.<init>(Unknown Source)
at sun.www.http.HttpClient.New(Unknown Source)
at sun.www.http.HttpClient.New(Unknown Source)
at sun.www.protocol.NewHttpClient(Unknown Source)
at sun.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.www.protocol.t(Unknown Source)
at sun.www.protocol.InputStream(Unknown Source)
at s.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at s.internal.impl.XMLEntityManager.startEntity(Unknown Source)
at s.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at s.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at s.internal.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at s.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at s.internal.parsers.XML11Configuration.parse(Unknown Source)
at s.internal.parsers.XML11Configuration.parse(Unknown Source)
at s.internal.parsers.XMLParser.parse(Unknown Source)
at s.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.ad(SAXReader.java:465)
at org.dom4j.ad(SAXReader.java:264)
...
Nested exception: java.UnknownHostException:
at t(Unknown Source)
at t(Unknown Source)
at t(Unknown Source)
at sun.NetworkClient.doConnect(Unknown Source)
at sun.www.http.HttpClient.openServer(Unknown Source)
at sun.www.http.HttpClient.openServer(Unknown Source)
at sun.www.http.HttpClient.<init>(Unknown Source)
at sun.www.http.HttpClient.New(Unknown Source)
使用dom4j解析xml文件at sun.www.http.HttpClient.New(Unknown Source)
at sun.www.protocol.NewHttpClient(Unknown Source)
at sun.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.www.protocol.t(Unknown Source)
at sun.www.protocol.InputStream(Unknown Source)
at s.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at s.internal.impl.XMLEntityManager.startEntity(Unknown Source)
at s.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at s.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at s.internal.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Un
known Source)
at s.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at s.internal.parsers.XML11Configuration.parse(Unknown Source)
at s.internal.parsers.XML11Configuration.parse(Unknown Source)
at s.internal.parsers.XMLParser.parse(Unknown Source)
at s.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.ad(SAXReader.java:465)
at org.dom4j.ad(SAXReader.java:264)
粗体标注的是异常的关键信息,看到此问题相信很多朋友都知道是因为下载doctype中的dtd对xml进⾏验证时,⽹络不通所致,sqlmap中doctype 定义如下:
<!DOCTYPE sqlMap PUBLIC "-////DTD SQL Map 2.0//EN" "/dtd/sql-map-2.dtd">
但实际情况是很多时候程序运⾏的环境⽹络不⼀定和互联⽹是通的,⽽且像这样每次联⽹去下载dtd也会对程序的效率产⽣影响.于是,出现两种解决办法:
1、去掉xml⽂件中的doctype或者将doctype中指定的互联⽹dtd⽂件改为引⽤本地dtd⽂件
2、对dom4j的SAXReader进⾏设置,不去下载外部dtd⽂件来对xml进⾏验证
相信⼤部分同仁都不⼤会同意第⼀种办法,呵呵,我也不太赞同。于是开始寻第⼆种⽅案的解决办法:
reader.setValidation(false);
reader.setFeature("/xml/features/nonvalidating/load-external-dtd", false); 两⾏代码解决问题,关键是第⼆⾏。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论