今天使用dom4j解析.xml的时候, 前提是我自定义了一个.xml文件.
大致内容如下:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <project> <name> <menu name="Student Management"> <node>/project/test.action</node> </menu> <menu name="Deploy Management"> <node>/project/selectBar.jsp</node> </menu> <menu name="view" value="/mainView.jsp"> <node>/project/infoView.jsp</node> </menu> </name> </project>
居然报了一个 连接超时的错误:
org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at com.util.ReadXmlDataUtil.getLanguageXMLData(ReadXmlDataUtil.java:28)
at com.admin.util.ReadXmlDataUtil.main(ReadXmlDataUtil.java:50)
Nested exception:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at com.huawei.ngbss.admin.util.ReadXmlDataUtil.getLanguageXMLData(ReadXmlDataUtil.java:28)
at com.huawei.ngbss.admin.util.ReadXmlDataUtil.main(ReadXmlDataUtil.java:50)
Nested exception: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
解决方案:
后面找到了其中的原因:
是网络的问题.因为DOCTYPE的连接不同造成的.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
使用了saxreader解析器的时候,会自动解析DOCTYPE里面的内容.如果连接不上网络,则会报连接超时的异常.
所以为了,不然解析DOCTYPE,则reader.setFeature(不加载dtd文件,false); 就不会去解析DOCTYPE了.
即可.
SAXReader reader = new SAXReader();
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);