6

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)

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

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

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>

Please help.

M.J.
  • 16,266
  • 28
  • 75
  • 97
  • Got the same problem today, the answers here may work, but it is not necessary to disable validation, because Hibernate can resolve DTDs locally (see this [answer](https://stackoverflow.com/a/8731499/4436313)). My DTD link was wrong: `http://hibernate.org` instead of `http://www.hibernate.org`. – DaRich Jul 28 '17 at 09:04

3 Answers3

16

The parser is trying to download the DTD from hibernate.sourceforge.net in order to validate the parsed 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).

To avoid this problem, you have to tell the DocumentBuilderFactory to ignore the 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);

See Make DocumentBuilder.parse ignore DTD references.

Community
  • 1
  • 1
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • is there any way to stop the parser to download the DTD. – M.J. Oct 23 '10 at 07:29
  • http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setValidating(boolean) – dvhh Oct 23 '10 at 07:50
  • @frederic: hi i tried with code snippet that you gave me but now i am getting some other error. java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V – M.J. Oct 23 '10 at 11:28
  • Looks like your document builder factory does not support `setFeature()`. Does that work with only `dbf.setValidating(false);`? – Frédéric Hamidi Oct 23 '10 at 11:40
  • @Frederic.. i am not using any other jars for this.. i am using normal rt.jar. Do i have to add some other jar for the same... – M.J. Oct 25 '10 at 04:00
2

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);
M.J.
  • 16,266
  • 28
  • 75
  • 97
-1

I am also trying read from a xml file with dtd tag

<!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.

senthilraja
  • 167
  • 2
  • 11