解析xml文件时未知的主机异常 [英] unknown host exception while parsing an xml file

查看:132
本文介绍了解析xml文件时未知的主机异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解析xml时,出现以下异常:-

when i am trying to parse an xml, i am getting following exception :-

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的代码如下:-

The code that i am using to parse the xml is below:-

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

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

i am trying to parse the xml that has been written for hibernate, actually it is a hibernate mapping file.

我要解析的xml如下:-

The xml that i am trying to parse is below:-

<?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>

请帮助.

推荐答案

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

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

这篇关于解析xml文件时未知的主机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆