紫風亂寫

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

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

          Posted on 2006-02-04 23:19 Justfly Shi 閱讀(790) 評論(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
          呵呵,高興:)
          主站蜘蛛池模板: 正定县| 平远县| 铜鼓县| 巴林左旗| 新津县| 武清区| 微博| 余庆县| 泸西县| 新郑市| 金山区| 新巴尔虎右旗| 新安县| 民和| 香格里拉县| 乐昌市| 松滋市| 林甸县| 中西区| 铜鼓县| 辛集市| 绩溪县| 宜宾县| 彰化县| 蛟河市| 乐陵市| 阳新县| 玉屏| 星座| 中阳县| 武强县| 广宗县| 奈曼旗| 阿克苏市| 淅川县| 广饶县| 横峰县| 虹口区| 松原市| 宁陕县| 杂多县|