紫風亂寫

          除了他眼前的屏幕,這個人什么也沒看見。
          被周圍的電腦簇擁著,他只知道他所創造的現實,但又意識到那是虛幻。
          他已經超越了技術。也超越了機器。
          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
          呵呵,高興:)
          主站蜘蛛池模板: 余干县| 广汉市| 南和县| 鹤岗市| 商丘市| 康定县| 建昌县| 娄底市| 繁峙县| 长治县| 宜阳县| 东莞市| 札达县| 阿克苏市| 乌鲁木齐市| 东明县| 从江县| 始兴县| 含山县| 温泉县| 思南县| 广饶县| 逊克县| 华亭县| 绵竹市| 铁岭市| 鹤山市| 迁安市| 佛山市| 洛宁县| 泽普县| 江陵县| 精河县| 东海县| 远安县| 神农架林区| 贵定县| 霍林郭勒市| 巴彦淖尔市| 盱眙县| 哈尔滨市|