Java SAX Parser raises UnknownHostException

岁酱吖の 提交于 2019-12-12 09:37:41

问题


The XML file I want to parse starts with :

<!DOCTYPE plist PUBLIC "-//...//DTD PLIST 1.0//EN" "http://www.....dtd">

So when I start the SAX praser, it tries to access this DTD online, and I get a java.net.UnknownHostException.

  1. I cannot modify the XML file before feeding it to the SAX parser
  2. I have to run even with no internet connection

How can I change the SAX Parser behaviour so that it does not try to load the DTD ? Thanks.

javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setValidating(false);

javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.parse(xmlFile, handler);

回答1:


Ok, turns out the parse() method overrides any previously set entity resolvers with the handler passed in to the parse method. The following code should work:

javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setValidating(false);
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.parse(new java.io.File("x.xml"), new org.xml.sax.helpers.DefaultHandler(){
        public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
                throws org.xml.sax.SAXException, java.io.IOException {
            System.out.println("Ignoring: " + publicId + ", " + systemId);
            return new org.xml.sax.InputSource(new java.io.StringReader(""));
        }
    }); 



回答2:


Use XMLReader instead of SAXParser.

    XMLReader reader =XMLReaderFactory.createXMLReader();
 reader.setEntityResolver(new DummyEntityResolver());
    reader.setContentHandler(handler);
 reader.parse(inputSource);

It should also work with SAXParser, but for some reasons it doesn't.




回答3:


You can implement a custom EntityResolver which is what is used to lookup external entities during XML parsing.

org.xml.sax.EntityResolver customEntityResolver = new DummyEntityResolver();
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.getXMLReader().setEntityResolver(customEntityResolver);
parser.parse(xmlFile, handler);

And in your custom EntityResolver, just always return null. I think that should fix this problem.




回答4:


You should provide an EntityResolve to have the problem resolve. I will recommend you to write a resolver that will know how to read the DTDs locally instead (provided that you have them shipped together with your application). Otherwise, return null like Gowri suggested.

You might want to read up the the API doc.

yc



来源:https://stackoverflow.com/questions/295766/java-sax-parser-raises-unknownhostexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!