when i am trying to parse an xml, i am getting following exception :-
java.net.UnknownHostException: hibernate.sourceforge.net
at java.net.AbstractPlainS
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.
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);
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.