沉睡森林@漂在北京

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

            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

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


          網站導航:
           
          主站蜘蛛池模板: 浦江县| 长乐市| 鹿邑县| 芦溪县| 开平市| 镇江市| 邯郸市| 鹰潭市| 云阳县| 柏乡县| 宁乡县| 绥江县| 万全县| 神池县| 义马市| 花垣县| 宣威市| 永靖县| 桐庐县| 连平县| 江陵县| 重庆市| 兴仁县| 通城县| 遂溪县| 五寨县| 潼南县| 宁海县| 和田县| 阿尔山市| 政和县| 南昌县| 岫岩| 连州市| 高碑店市| 沂水县| 赤峰市| 鲁甸县| 孟连| 阳信县| 资中县|