Java SAX Parser raises UnknownHostException

独自空忆成欢 提交于 2019-12-05 05:36:04

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(""));
        }
    }); 
Stefano Rosanelli

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.

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.

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

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