沉睡森林@漂在北京

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

            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 王總兵 閱讀(576) 評論(0)  編輯  收藏 所屬分類: Digester

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


          網站導航:
           
          主站蜘蛛池模板: 台前县| 陕西省| 清涧县| 石渠县| 孟连| 北宁市| 双辽市| 托克托县| 阿荣旗| 锡林浩特市| 南城县| 绩溪县| 柳林县| 大理市| 青田县| 平定县| 麻栗坡县| 恩平市| 玉龙| 垦利县| 萨嘎县| 珲春市| 黔西县| 漾濞| 连江县| 吴旗县| 涞水县| 常山县| 分宜县| 景德镇市| 开平市| 梁平县| 苍梧县| 岳阳县| 河池市| 巴南区| 海南省| 宣恩县| 通化县| 宜川县| 招远市|