沉睡森林@漂在北京

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

            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

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


          網站導航:
           
          主站蜘蛛池模板: 海安县| 吉林市| 蒙阴县| 临泽县| 珠海市| 罗山县| 潜山县| 南雄市| 玛纳斯县| 大关县| 永修县| 九龙坡区| 朝阳区| 米脂县| 玉屏| 锡林郭勒盟| 辽中县| 天津市| 元江| 呼图壁县| 尉犁县| 河西区| 喀喇| 桂东县| 阳泉市| 南雄市| 河北区| 双桥区| 太和县| 晋宁县| 普兰店市| 德格县| 廊坊市| 马山县| 青海省| 平陆县| 武邑县| 大港区| 甘南县| 西乌珠穆沁旗| 垫江县|