Unknown host exception while parsing xml file

when I try to parse xml, I get the 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 I'm 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 was written for hibernation, it is actually a hibernation display file.

The xml I'm 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.

+4
source share
3 answers

I used the following code and it works 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); 
+2
source

The parser tries to download the DTD from hibernate.sourceforge.net to check the parsed XML.

However, the DNS client on the computer cannot resolve this hostname for any reason (it allows a fine of up to 82.98.86.175 on my machine).

To avoid this problem, you should tell DocumentBuilderFactory ignore 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 links .

+14
source

I am also trying to read from an XML file with the 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 on which the code is deployed does not have access to w3.org. When I open w3.org in a browser, it opens a page with a disconnected connection. I gave access to w3.org and it solved the problem.

0
source

All Articles