Posted on 2006-02-04 23:19
Justfly Shi 阅读(784)
评论(3) 编辑 收藏 所属分类:
tips
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.