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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南京市| 天门市| 德化县| 永善县| 洛宁县| 乌兰县| 乐昌市| 西安市| 同江市| 南充市| 犍为县| 台中市| 墨江| 万荣县| 高清| 石家庄市| 明溪县| 永新县| 静安区| 沁源县| 和政县| 惠州市| 新泰市| 黑河市| 长阳| 娄底市| 建宁县| 延安市| 新泰市| 黄骅市| 桓台县| 曲松县| 双鸭山市| 澄江县| 承德市| 南安市| 贵港市| 文山县| 城固县| 连山| 东乡|