紫風亂寫

          除了他眼前的屏幕,這個人什么也沒看見。
          被周圍的電腦簇擁著,他只知道他所創造的現實,但又意識到那是虛幻。
          他已經超越了技術。也超越了機器。
          posts - 62, comments - 93, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          如何讓jdom不對xml文件進行驗證

          Posted on 2006-02-04 23:19 Justfly Shi 閱讀(795) 評論(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
          呵呵,高興:)
          主站蜘蛛池模板: 洛南县| 池州市| 平泉县| 隆德县| 光山县| 息烽县| 四子王旗| 和平县| 华阴市| 方山县| 确山县| 遂宁市| 盖州市| 山西省| 绩溪县| 连江县| 唐山市| 石河子市| 轮台县| 克拉玛依市| 行唐县| 和政县| 沙洋县| 二连浩特市| 马公市| 池州市| 行唐县| 石城县| 伊吾县| 将乐县| 天峨县| 武乡县| 红河县| 合作市| 凉山| 黎川县| 余干县| 高青县| 连城县| 龙口市| 绍兴市|