使用dom4j解析自定义的.xml文件.
大致内容如下:
居然报了一个 连接超时的错误:
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 plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.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);