unknown host exception while parsing an xml file

前端 未结 3 464
粉色の甜心
粉色の甜心 2021-01-18 09:30

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

java.net.UnknownHostException: hibernate.sourceforge.net
    at java.net.AbstractPlainS         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 10: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.

提交回复
热议问题