java學習

          java學習

           

          java--dom讀取xml文件

           public Map readXml() {
                  try {
                      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                      DocumentBuilder builder = dbf.newDocumentBuilder();
                      Document doc;
                      InputStream stream = this.getClass().getResourceAsStream("/dictionaries.xml");
                      doc = builder.parse(stream);
                      Element root = doc.getDocumentElement();
                      NodeList list = root.getElementsByTagName("dictionary");
                      Map map = new LinkedHashMap();
                      ArrayList<String> types = new ArrayList<String>();
                      for (int i = 0; i < list.getLength(); i++) {
                          String key = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
                          String value = list.item(i).getAttributes().getNamedItem("value").getNodeValue();
                          map.put(key, value);
                      }
                      return map;
                  } catch (Exception ex) {
                      return null;
                  }

              }

          dictionaries.xml文件如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <dictionaries>
              <dictionary name="項目類型" value="project.types" />
              <dictionary name="項目狀態" value="project.statuses" />
              <dictionary name="任務階段" value="task.stages" />
              <dictionary name="任務狀態" value="task.statuses" />
              <dictionary name="發生幾率" value="risk.probability" />
              <dictionary name="危險程度" value="risk.harm" />
              <dictionary name="風險狀態" value="risk.status" />
              <dictionary name="預案類別" value="risk.type" />
              <dictionary name="合同類型" value="contract.type" />
              <dictionary name="合同狀態" value="contract.status" />
          </dictionaries>

          posted on 2013-07-09 11:14 楊軍威 閱讀(357) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 兴和县| 温宿县| 乌审旗| 巢湖市| 武平县| 板桥市| 偏关县| 建始县| 聂荣县| 成安县| 武平县| 漳平市| 沅江市| 湘乡市| 旺苍县| 长宁县| 盐津县| 万盛区| 安庆市| 肥乡县| 台中市| 汝南县| 徐州市| 芒康县| 嵊泗县| 石景山区| 江达县| 普陀区| 高陵县| 大余县| 甘孜| 武宁县| 合阳县| 兰西县| 沙田区| 锡林郭勒盟| 楚雄市| 宽城| 循化| 登封市| 聂拉木县|