<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    想飛就別怕摔

    大爺的并TM罵人

    JDom解析xml學習筆記

    學習中發現兩個哥們寫的挺好的。轉過來方便以后使用。謝謝那倆哥們了。
    http://wuhongyu.iteye.com/blog/361842
     xml是一種廣為使用的可擴展標記語言,java中解析xml的方式有很多,最常用的像jdom、dom4j、sax等等。前兩天剛好有個程序需要解析xml,就學了下jdom,寫了個小例子,這里做個學習筆記。

     

        要使用jdom解析xml文件,需要下載jdom的包,我使用的是jdom-1.1。解壓之后,將lib文件夾下的.jar文件以及build文件夾下的jdom.jar拷貝到工程文件夾下,然后就可以使用jdom操作xml文件了。

     

        一、讀取xml文件

     

        假設有這樣一個xml文件:

    Xml代碼 復制代碼 收藏代碼
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <sys-config>  
    3.     <jdbc-info>  
    4.         <driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>  
    5.         <url>jdbc:oracle:thin:@localhost:1521:database</url>  
    6.         <user-name>why</user-name>  
    7.         <password>why</password>  
    8.     </jdbc-info>  
    9.     <provinces-info>  
    10.         <province id="hlj" name="黑龍江">  
    11.             <city id="harb">哈爾濱</city>  
    12.             <city id="nj">嫩江</city>  
    13.         </province>  
    14.         <province id="jl" name="吉林"></province>  
    15.     </provinces-info>  
    16. </sys-config>  

     

        首先,用 org.jdom.input.SAXBuilder 這個類取得要操作的xml文件,會返回一個 org.jdom.Document 對象,這里需要做一下異常處理。然后,取得這個xml文件的根節點,org.jdom.Element 代表xml文件中的一個節點,取得跟節點后,便可以讀取xml文件中的信息。利用 org.jdom.xpath.XPath 可以取得xml中的任意制定的節點中的信息。

        例如,要取得上面文件中的 <jdbc-info> 下的 <driver-class-name> 中的內容,先取得這個節點Element driverClassNameElement = (Element)XPath.selectSingleNode(rootEle, "http://sys-config/jdbc-info/driver-class-name"),注意,根節點前要使用兩個 "/" ,然后,用 driverClassNameElement.getText() 便可以取得這個節點下的信息。

        如果一個節點下有多個名稱相同的子節點,可以用XPath.selectNodes()方法取得多個子節點的List,遍歷這個List就可以操作各個子節點的內容了。

        下面是我寫的讀取上面xml文件的例子,比起文字描述更直觀一些吧:

    Java代碼 復制代碼 收藏代碼
    1. package com.why.jdom;   
    2.   
    3. import java.io.IOException;   
    4. import java.util.Iterator;   
    5. import java.util.List;   
    6.   
    7. import org.jdom.input.SAXBuilder;   
    8. import org.jdom.xpath.XPath;   
    9. import org.jdom.Document;   
    10. import org.jdom.Element;   
    11. import org.jdom.JDOMException;   
    12.   
    13. public class ReadXML {   
    14.   
    15.     /**  
    16.      * @param args  
    17.      */  
    18.     public static void main(String[] args) {   
    19.         SAXBuilder sax = new SAXBuilder();   
    20.         try {   
    21.             Document doc = sax.build("src/config.xml");   
    22.             Element rootEle = doc.getRootElement();   
    23.             Element driverClassNameElement = (Element)XPath.selectSingleNode(rootEle, "http://sys-config/jdbc-info/driver-class-name");   
    24.             String driverClassName = driverClassNameElement.getText();   
    25.             System.out.println("driverClassName = " + driverClassName);   
    26.                
    27.             List provinceList = XPath.selectNodes(rootEle, "http://sys-config/provinces-info/province");   
    28.             for(Iterator it = provinceList.iterator();it.hasNext();){   
    29.                 Element provinceEle = (Element)it.next();   
    30.                 String proId = provinceEle.getAttributeValue("id");   
    31.                 String proName = provinceEle.getAttributeValue("name");   
    32.   
    33.                 System.out.println("provinceId = " + proId + "   provinceName = " + proName);   
    34.                    
    35.                 List cityEleList = (List)provinceEle.getChildren("city");   
    36.                    
    37.                 for(Iterator cityIt = cityEleList.iterator();cityIt.hasNext();){   
    38.                     Element cityEle = (Element)cityIt.next();   
    39.                     String cityId = cityEle.getAttributeValue("id");   
    40.                     String cityName = cityEle.getText();   
    41.   
    42.                     System.out.println("    cityId = " + cityId + "   cityName = " + cityName);   
    43.                 }   
    44.             }   
    45.         } catch (JDOMException e) {   
    46.             // TODO 自動生成 catch 塊   
    47.             e.printStackTrace();   
    48.         } catch (IOException e) {   
    49.             // TODO 自動生成 catch 塊   
    50.             e.printStackTrace();   
    51.         }   
    52.   
    53.     }   
    54.   
    55. }  

     

     

        二、寫xml文件

     

        寫xml文件與讀取xml文件的操作類似,利用 org.jdom.output.XMLOutputter 就可以將處理好的xml輸出到文件了。可以設置文件的編碼方式,不過一般使用UTF-8就可以了。代碼如下:

     

    Java代碼 復制代碼 收藏代碼
    1. package com.why.jdom;   
    2.   
    3. import java.io.FileNotFoundException;   
    4. import java.io.FileOutputStream;   
    5. import java.io.IOException;   
    6.   
    7. import org.jdom.Document;   
    8. import org.jdom.Element;   
    9. import org.jdom.output.XMLOutputter;   
    10.   
    11. public class WriteXML {   
    12.   
    13.            
    14.     /**  
    15.      * @param args  
    16.      */  
    17.     public static void main(String[] args) {   
    18.         // TODO 自動生成方法存根   
    19.         Element rootEle = new Element("sys-config");   
    20.         Element provincesEle = new Element("provinces-info");   
    21.            
    22.         Element provinceEle = new Element("province");   
    23.         provinceEle.setAttribute("id","hlj");   
    24.         provinceEle.setAttribute("name","黑龍江省");   
    25.            
    26.         Element cityEle1 = new Element("city");   
    27.         cityEle1.setAttribute("id","harb");   
    28.         cityEle1.addContent("哈爾濱");   
    29.            
    30.         Element cityEle2 = new Element("city");   
    31.         cityEle2.setAttribute("id","nj");   
    32.         cityEle2.addContent("嫩江");   
    33.            
    34.            
    35.         provinceEle.addContent(cityEle1);   
    36.         provinceEle.addContent(cityEle2);   
    37.         provincesEle.addContent(provinceEle);   
    38.         rootEle.addContent(provincesEle);   
    39.            
    40.         Document doc = new Document(rootEle);   
    41.            
    42.         XMLOutputter out = new XMLOutputter();   
    43.            
    44.            
    45. //      out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//設置文件編碼,默認為UTF-8   
    46.         String xmlStr = out.outputString(doc);   
    47.         System.out.println(xmlStr);   
    48.            
    49.         try {   
    50.             out.output(doc, new FileOutputStream("c:/test.xml"));   
    51.         } catch (FileNotFoundException e) {   
    52.             // TODO 自動生成 catch 塊   
    53.             e.printStackTrace();   
    54.         } catch (IOException e) {   
    55.             // TODO 自動生成 catch 塊   
    56.             e.printStackTrace();   
    57.         }   
    58.            
    59.     }   
    60.   
    61. }  

    http://www.cnblogs.com/ling_yun/archive/2011/01/19/1939674.html

    下面是xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <persons>
     <person perid="1001">
      <name>lhu</name>
      <age>89</age>
      <address>安徽淮北</address>
      <sex>男</sex>
     </person>
     
     <person perid="1002">
      <name>we</name>
      <age>56</age>
      <address>北京海淀</address>
      <sex>女</sex>
     </person>
    </persons>

     

    下面是解析上面的xml文件:

    通過JDOM來解析,需要借助第三方的組件.jdom.jar,網上有1.0的版本下載

    package cn.com.jdom;

    import java.io.File;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;

    import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.ProcessingInstruction;
    import org.jdom.input.SAXBuilder;

    import com.sun.xml.internal.bind.v2.runtime.Name;

    /**
     * jdom解析xml文件
     *
     * @author ly *
     */
    public class JDomXML {

     public JDomXML() {
     }

     /**
      * 解析xml文件
      * @param xmlFile
      */
     public void parseXml(File xmlFile) {
      SAXBuilder sax = new SAXBuilder();//在內存中建立一個sax文檔模型
      try {
       Document xmlDom = sax.build(xmlFile);//創建文檔
       //獲得文件的根元素
       Element root = xmlDom.getRootElement();
       System.out.println("根元素是:"+root.getName());
       
       //獲得根元素的子節點
       List childList = root.getChildren();
       Iterator listIt = childList.iterator();
       while(listIt.hasNext()){
        Element element = (Element)listIt.next();
        System.out.println("孩子結點是:"+element.getName());
       }
       
       //獲得第一個孩子結點
       Element firstChild = (Element) childList.get(0);
       //獲得孩子結點的屬性
       List attrList = firstChild.getAttributes();
       Iterator attrIt = attrList.iterator();
       while(attrIt.hasNext()){
        Attribute  attr = (Attribute ) attrIt.next();
        System.out.println("第一個元素的屬性是:"+attr.getName());
        //獲得屬性的值
        System.out.println("屬性的值是:"+attr.getValue());
        //獲得屬性的類型
        System.out.println("屬性的類型是:"+attr.getAttributeType());
       }
       
       List sonList = firstChild.getChildren();
       Iterator sonIt = sonList.iterator();
       while(sonIt.hasNext()){
        Element temp = (Element)sonIt.next();
        System.out.println("屬性"+temp.getName()+"的值是:"+temp.getValue());
       }
       
       
      } catch (JDOMException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public static void main(String[] args) {
      JDomXML test = new JDomXML();
      test.parseXml(new File("persons.xml"));
     }
    }

    posted on 2011-08-17 10:51 生命的綻放 閱讀(536) 評論(0)  編輯  收藏 所屬分類: JAVA

    <2011年8月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導航

    統計

    常用鏈接

    留言簿(5)

    隨筆分類(94)

    隨筆檔案(93)

    文章分類(5)

    文章檔案(5)

    相冊

    JAVA之橋

    SQL之音

    兄弟之窗

    常用工具下載

    積分與排名

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 色欲国产麻豆一精品一AV一免费| 100000免费啪啪18免进| 国产成人综合久久精品免费| 亚洲视频小说图片| 在线看片免费人成视久网| 亚洲免费视频在线观看| 6080午夜一级毛片免费看 | 亚洲精品无码不卡在线播HE| 亚洲AV永久无码精品一福利| 国内一级一级毛片a免费| 亚洲第一成年网站视频| 亚洲av麻豆aⅴ无码电影| 一区二区免费在线观看| 亚洲日韩欧洲无码av夜夜摸| 亚洲免费人成在线视频观看 | 暖暖日本免费在线视频| 精品久久久久亚洲| 国产成人99久久亚洲综合精品| 亚洲成A人片在线观看无码不卡| 亚洲男人天堂2022| 日韩免费三级电影| 一级有奶水毛片免费看| 亚洲AV无码久久| 999在线视频精品免费播放观看| 国产精品亚洲αv天堂无码| 久草免费手机视频| 亚洲日韩精品国产一区二区三区| av永久免费网站在线观看 | 久久精品视频免费看| 亚洲最大免费视频网| 天天天欲色欲色WWW免费| 成人国产网站v片免费观看| 无码专区—VA亚洲V天堂| 成人免费毛片观看| 亚欧乱色国产精品免费视频| 亚洲嫩草影院久久精品| 国产免费观看黄AV片| 四虎国产成人永久精品免费| 亚洲精品无码av片| 亚洲国产女人aaa毛片在线| 免费看又爽又黄禁片视频1000|