java學習

          java學習

           

          dom4j解析xml文件

          1.先導入dom4j-1.6.1.jar包
          2.xml文件如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <config>
           
           
           <action name="user" className="com.kaishengit.web.UserAction">
            <result name="success" type="forward">suc.jsp</result>
            <result name="error" type="redirect">404.jsp</result>
           </action>
           
           
           <action name="book" className="com.kaishengit.web.BookAction">
            <result name="success">book.jsp</result>
            <result name="error" type="redirect">bookerror.jsp</result>
           </action>
           
           <action name="person" className="com.kaishengit.web.PersonAction" method="del">
            <result name="ok">suc.jsp</result>
           </action>
           
           

          </config>

          3.解析測試類是:

          package com.kaishengit.test;

          import java.io.File;
          import java.net.URL;
          import java.util.List;

          import org.dom4j.Document;
          import org.dom4j.Element;
          import org.dom4j.io.SAXReader;

          public class Dom4jTest {

           public void readXML() {
            //拿到src文件夾里的xml配置文件
            URL url = getClass().getResource("/");
            System.out.println(url);
            String filePath = url.getFile() + "struts.xml";
            
            try {
             //創建讀取配置文件的對象
             SAXReader reader = new SAXReader();
             //開始讀取配置文件
             Document doc = reader.read(new File(filePath));
             //拿到根節點
             Element root = doc.getRootElement();
             
             //拿到根節點下的action接點數組
             List<Element> actions = root.elements("action");
             
             for(Element action : actions) {
              String name = action.attributeValue("name");
              String className = action.attributeValue("className");
              String method = action.attributeValue("method");
              System.out.println("name="+name);
              System.out.println("className="+className);
              System.out.println("method="+method);
              
              List<Element> results = action.elements("result");
              for(Element result : results) {
               String resultName = result.attributeValue("name");
               String resultType = result.attributeValue("type");
               String pageName = result.getText();
               
               System.out.println("name:" + resultName + "\tresultType:" + resultType + "\tpageName:" + pageName);
             
              }
              
              System.out.println("----------------------");
             }
             
             
            } catch (Exception e) {
             e.printStackTrace();
            }
            
           }
           
           
           
           public static void main(String[] args) {
            
            
            Dom4jTest d = new Dom4jTest();
            d.readXML();
            
            
            
           }
          }

          posted on 2013-05-06 21:08 楊軍威 閱讀(1352) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富裕县| 中西区| 芷江| 惠来县| 博兴县| 黄大仙区| 武城县| 鄯善县| 新宾| 鄂托克前旗| 靖宇县| 项城市| 确山县| 临泉县| 东至县| 海城市| 平遥县| 阿克陶县| 光山县| 新民市| 马关县| 樟树市| 大洼县| 游戏| 宜宾市| 汾阳市| 华亭县| 婺源县| 边坝县| 平潭县| 台山市| 通州区| 中牟县| 修文县| 道真| 武山县| 上饶市| 安溪县| 曲阜市| 简阳市| 咸宁市|