How do I turn off validation when parsing well-formed XML using DocumentBuilder.parse?

允我心安 提交于 2019-12-04 04:22:11
Alexis Dufrenoy

Probably related to an EntityResolver issue. You can take a look here:

How to read well formed XML in Java, but skip the schema?

Here is how you create a DocumentBuilder that will ignore ALL external referenced entities, including DTDs:

final DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
    @Override
        public InputSource resolveEntity(String publicId, String systemId) {
                // it might be a good idea to insert a trace logging here that you are ignoring publicId/systemId
                return new InputSource(new StringReader("")); // Returns a valid dummy source
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!