java 解析 xml 文件时出现未知主机异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4002885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unknown host exception while parsing an xml file
提问by M.J.
when i am trying to parse an xml, i am getting following exception :-
当我尝试解析 xml 时,出现以下异常:-
java.net.UnknownHostException: hibernate.sourceforge.net
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
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)
The code that i am using to parse the xml is below:-
我用来解析 xml 的代码如下:-
File hbmFile = new File(hbmFileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(hbmFile);
i am trying to parse the xml that has been written for hibernate, actually it is a hibernate mapping file.
我正在尝试解析为 hibernate 编写的 xml,实际上它是一个 hibernate 映射文件。
The xml that i am trying to parse is below:-
我试图解析的 xml 如下:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.entity.Student" table="table_student">
<id name="rollNo" column="rool_no" type="int"/>
<property name="name" column="st_name" type="string"/>
<set name="marks" table="table_marks">
<key column="roll_no"/>
<composite-element class="org.hibernate.entity.StudentMarks">
<property name="subject" column="st_sub"/>
<property name="marks" column="st_marks"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
Please help.
请帮忙。
采纳答案by M.J.
i used the following code and this is working fine for me..
我使用了以下代码,这对我来说很好用..
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String arg0, String arg1)
throws SAXException, IOException {
if(arg0.contains("Hibernate")) {
return new InputSource(new StringReader(""));
} else {
// TODO Auto-generated method stub
return null;
}
}
});
Document doc = db.parse(hbmFile);
回答by Frédéric Hamidi
The parser is trying to download the DTDfrom hibernate.sourceforge.net
in order to validate the parsed XML.
解析器正试图下载DTD从hibernate.sourceforge.net
以验证已解析的XML。
However, the DNS client on the machine can't resolve that host name for some reason (it resolves fine to 82.98.86.175
on my machine).
但是,由于某种原因,机器上的 DNS 客户端无法解析该主机名(它82.98.86.175
在我的机器上解析得很好)。
To avoid this problem, you have to tell the DocumentBuilderFactory
to ignore the DTD:
为了避免这个问题,你必须告诉DocumentBuilderFactory
忽略 DTD:
File hbmFile = new File(hbmFileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(hbmFile);
回答by senthilraja
I am also trying read from a xml file with dtd tag
我也在尝试从带有 dtd 标签的 xml 文件中读取
<!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0//EN" "http://www.w3.org/TR/speech-grammar/grammar.dtd">
and code
和代码
File fXmlFile = new File(grammarXML);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
I was getting the same error.
我遇到了同样的错误。
java.net.UnknownHostException:
The server where the code is deployed does not have access to w3.org. When i open w3.org in a browser, it is opening connection timed out page. I gave access to w3.org and it solved the issue.
部署代码的服务器无权访问 w3.org。当我在浏览器中打开 w3.org 时,它正在打开连接超时页面。我授予了 w3.org 的访问权限,它解决了这个问题。