【问题标题】:unknown host exception while parsing an xml file解析 xml 文件时出现未知主机异常
【发布时间】:2011-04-29 12:12:01
【问题描述】:

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

请帮忙。

【问题讨论】:

  • 今天遇到了同样的问题,这里的答案可能有效,但是没有必要禁用验证,因为 Hibernate 可以在本地解析 DTD(参见 answer)。我的 DTD 链接错误:http://hibernate.org 而不是 http://www.hibernate.org

标签: java xml parsing


【解决方案1】:

解析器正在尝试从hibernate.sourceforge.net 下载DTD 以验证已解析的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

【讨论】:

  • 有什么办法可以阻止解析器下载DTD。
  • @frederic:嗨,我尝试使用您给我的代码 sn-p,但现在我遇到了一些其他错误。 java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
  • 看起来您的文档生成器工厂不支持setFeature()。这只适用于dbf.setValidating(false);吗?
  • @Frederic.. 我没有为此使用任何其他 jars.. 我使用的是普通的 rt.jar。我是否必须为相同的添加一些其他 jar...
【解决方案2】:

我使用了以下代码,这对我来说很好..

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);

【讨论】:

    【解决方案3】:

    我也在尝试从带有 dtd 标记的 xml 文件中读取

    <!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0//EN" "http://www.w3.org/TR/speech-grammar/grammar.dtd">
    

    和代码

    File fXmlFile = new File(grammarXML);                   
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    

    我遇到了同样的错误。

    java.net.UnknownHostException:
    

    部署代码的服务器无权访问 w3.org。当我在浏览器中打开 w3.org 时,它正在打开连接超时页面。我授予了 w3.org 的访问权限,它解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多