posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2009年8月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 862642
          • 排名 - 44

          最新評論

          閱讀排行榜

          要這么做是因為Server返回給我們的XML肯定是合法的,不需要驗證。

          而設置不需要驗證,只需要設置DocumentBuilderFactory.setValidating(false)就可以達到效果了,但是解析器還是會讀取DTD的,解決的方法是實現EntityResolver接口,具體代碼如下:

          package com.founder.demo;

          import Java.io.ByteArrayInputStream;
          import Java.io.IOException;

          import org.xml.sax.EntityResolver;
          import org.xml.sax.InputSource;
          import org.xml.sax.SAXException;

          public class IgnoreDTDEntityResolver implements EntityResolver {

           @Override
           public InputSource resolveEntity(String publicId, String systemId)
             throws SAXException, IOException {
                  return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
           }

          }

          然后設置SAXReader 對象如下:
          SAXReader reader = new SAXReader();
          reader.setEntityResolver(new IgnoreDTDEntityResolver()); // ignore dtd

          一切ok。

          posted on 2009-08-06 11:17 周銳 閱讀(1883) 評論(2)  編輯  收藏 所屬分類: JavaXML
          主站蜘蛛池模板: 呈贡县| 日土县| 昌江| 靖西县| 通山县| 瓦房店市| 故城县| 老河口市| 昌江| 永泰县| 华坪县| 台南县| 桃园县| 宁河县| 类乌齐县| 伊吾县| 安岳县| 铜鼓县| 绍兴县| 平凉市| 诸城市| 阿尔山市| 大同市| 漳浦县| 水富县| 交城县| 镇巴县| 朝阳县| 南岸区| 齐齐哈尔市| 互助| 樟树市| 石阡县| 新河县| 修武县| 秭归县| 修文县| 榆中县| 伊金霍洛旗| 合川市| 仁怀市|