gpt4 book ai didi

java - 解析 xml 文件时出现未知主机异常

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:56 27 4
gpt4 key购买 nike

当我尝试解析 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)

我用来解析 xml 的代码如下:-

File hbmFile = new File(hbmFileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(hbmFile);

我正在尝试解析为 hibernate 编写的 xml,实际上它是一个 hibernate 映射文件。

我试图解析的 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>

请帮忙。

最佳答案

解析器正在尝试下载 DTD来自 hibernate.sourceforge.net 以验证解析的 XML。

但是,机器上的 DNS 客户端出于某种原因无法解析该主机名(它在我的机器上解析为 82.98.86.175)。

要避免这个问题,您必须告诉 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);

参见 Make DocumentBuilder.parse ignore DTD references .

关于java - 解析 xml 文件时出现未知主机异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4002885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com