Disable XML Entity resolving in JDOM / DOM

夙愿已清 提交于 2019-12-04 11:08:21

I recommend the JDOM FAQ:

[http://www.jdom.org/docs/faq.html#a0350]

How do I keep the DTD from loading? Even when I turn off validation the parser tries to load the DTD file.

Even when validation is turned off, an XML parser will by default load the external DTD file in order to parse the DTD for external entity declarations. Xerces has a feature to turn off this behavior named "http://apache.org/xml/features/nonvalidating/load-external-dtd" and if you know you're using Xerces you can set this feature on the builder.

builder.setFeature(
  "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

If you're using another parser like Crimson, your best bet is to set up an EntityResolver that resolves the DTD without actually reading the separate file.

import org.xml.sax.*;
import java.io.*;

public class NoOpEntityResolver implements EntityResolver {
  public InputSource resolveEntity(String publicId, String systemId) {
    return new InputSource(new StringBufferInputStream(""));
  }
}

Then in the builder...

builder.setEntityResolver(new NoOpEntityResolver());

There is a downside to this approach. Any entities in the document will be resolved to the empty string, and will effectively disappear. If your document has entities, you need to setExpandEntities(false) code and ensure the EntityResolver only suppresses the DocType.

I believe that if validation (feature http://xml.org/sax/features/validation) is true it overrides setExpandEntities(false). Try also disabling validation by setting that feature to false.

I've found various hints like this one that say that you can't turn off entity expansion in attributes. I'm not sure what to suggest that's not ugly. For example, you could use an EntityResolver that would bring in a "null" DTD - that defined the expansion of "wiki" as "&wiki;". Seems like there's gotta be a better way!

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