解析String XML文档时出现NoRouteToHostException

时间:2014-07-14 15:53:05

标签: java xml xml-parsing remote-access

我从远程位置获取XML文档。该文档以字符串形式返回。之后,我尝试使用java xml解析器解析String。但是,有时解析会以java.net.NoRouteToHostException中止。

我想知道异常来自世界的哪个地方,因为根据我的知识,我在抛出异常时使用String,而不是Stream或任何其他需要连接的数据格式。具有讽刺意味的是,在异常catch中,整个String的打印没有任何问题。

致电代码:

javax.xml.parsers.DocumentBuilder build = DocumentBuilderFactory.newInstance().newDocumentBuilder();
...

try{
    String deploymentDescriptor = (String) getRemoteDeploymentDescriptor(webModule);
    org.w3c.dom.Document doc = build.parse( new ByteArrayInputStream( deploymentDescriptor.getBytes() ) );
    ...

} catch(Exception e){
    log.error("Could not parse document '" + deploymentDescriptor + "', e);
}

日志条目:

[14.07.14 16:06:17:157 CEST] Could not parse document '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">
<application>
</application>'
                            java.net.NoRouteToHostException: Keine Route zum Zielrechner
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at java.net.Socket.connect(Socket.java:488)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:401)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:536)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
at sun.net.www.http.HttpClient.New(HttpClient.java:313)
at sun.net.www.http.HttpClient.New(HttpClient.java:330)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:982)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:923)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:848)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1184)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at <my calling code, line "build.parse(...)">

这个世界怎么可能?我认为Strings总是完全保存在当前的操作系统中(与InputStreams相反,它可能确实在中间的某个时间松开了它们的连接)。或者我在调用代码时做错了什么?

对于同一个远程位置的不同xml文件,多次执行调用代码中的try-catch块。它适用于大多数文件,但不是全部。重复实验时,它始终是不起作用的相同文件。但是,日志错误条目每次都会完美地打印整个文件。

1 个答案:

答案 0 :(得分:1)

这里的问题是,解析器尝试解析XML文档中引用的文档类型定义。因此,即使已加载XML文档,也会在处理文档时加载引用的DTD。 “无主机路由”表示存在临时网络问题或网络配置问题。

如果您设置了EntityResolver,则可以控制分辨率并处理错误,例如。通过缓存DTD或从本地副本提供DTD。您还可以配置本地系统XML目录,以防止XML解析器从网络加载实体。