沉睡森林@漂在北京

          本處文章除注明“轉載”外均為原創,轉載請注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks
              在利用digester處理XML文件時,讀取多個XML文件,代碼反復測試都出現錯誤。后來,經過幾個分鐘的查找,終于加入一段代碼讓程序跑通。代碼如下:
          package com.example.demo2;
          import java.io.File;
          import org.apache.commons.digester.Digester;
          public class DigesterDriver {
           public static void main(String[] args) {
            try {
             Digester digester = new Digester();
             File input = new File("E:\\MyProjects\\Workspace\\DigesterDemo\\src\\com\\example\\demo2\\example.xml");
             File input2 = new File("E:\\MyProjects\\Workspace\\DigesterDemo\\src\\com\\example\\demo2\\example2.xml");
             Catalog2 c = new Catalog2();
             digester.push(c);
             digester.setValidating(false);
             digester.addObjectCreate("catalog/hi/book", Book2.class);
             digester.addBeanPropertySetter("catalog/hi/book/author", "author");
             digester.addBeanPropertySetter("catalog/hi/book/title", "title");
             digester.addSetNext("catalog/hi/book", "addBook" );
             digester.parse(input);
             digester.push(c);
             digester.parse(input2);
             System.out.println(c.getBooks().size());
            } catch (Exception exc) {
             exc.printStackTrace();
            }
           }
          }
              其中,紅色部分是后來加入的,仔細閱讀了struts的部分代碼,發現了這個錯誤。網上對這個介紹比較少,可能我這個問題比較弱智吧。struts的代碼片段如下:

                  // Configure the Digester instance we will use
                  Digester digester = initConfigDigester();

                  // Process each specified resource path
                  while (paths.length() > 0) {
                      digester.push(config);
                      String path = null;
                      int comma = paths.indexOf(',');
                      if (comma >= 0) {
                          path = paths.substring(0, comma).trim();
                          paths = paths.substring(comma + 1);
                      } else {
                          path = paths.trim();
                          paths = "";
                      }

                      if (path.length() < 1) {
                          break;
                      }
                     
                      this.parseModuleConfigFile(prefix, paths, config, digester, path);
                  }


          posted on 2008-10-18 21:43 王總兵 閱讀(583) 評論(0)  編輯  收藏 所屬分類: Digester

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 隆林| 灌阳县| 青州市| 嘉义市| 岑溪市| 瑞昌市| 罗甸县| 庆元县| 日喀则市| 惠安县| 栖霞市| 冕宁县| 湛江市| 安陆市| 桐城市| 霞浦县| 依兰县| 化隆| 宁陕县| 衡阳县| 余姚市| 兴安盟| 潞西市| 丽水市| 麻栗坡县| 电白县| 宝坻区| 天全县| 乌海市| 鄂州市| 霸州市| 寻乌县| 高碑店市| 武陟县| 西畴县| 东丽区| 临汾市| 甘南县| 安陆市| 苍山县| 永城市|