紫风乱写

除了他眼前的屏幕,这个人什么也没看见。
被周围的电脑簇拥着,他只知道他所创造的现实,但又意识到那是虚幻。
他已经超越了技术。也超越了机器。
posts - 62, comments - 93, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

如何让jdom不对xml文件进行验证

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.


评论

# re: 如何让jdom不对xml文件进行验证  回复  更多评论   

2006-07-04 11:17 by wj
谢谢,遇到了这个问题,通过您这篇文章解决了

# re: 如何让jdom不对xml文件进行验证  回复  更多评论   

2006-08-22 22:15 by 李生
呵呵,太感谢你了,今天遇到了这个问题,没想到在你这找到答案了,谢谢

# re: 如何让jdom不对xml文件进行验证  回复  更多评论   

2006-08-30 23:31 by Justfly Shi
呵呵,高兴:)

只有注册用户登录后才能发表评论。


网站导航: