JDOM 操作XML

XML是數據領域的Java語言,它使數據獨立於創建它的軟件和該軟件所在的操作系統,就像Java使軟件獨立於操作系統一樣。

“Jdom makes xml easy said by jason hunter.

Jdom是用Java語言讀、寫、操作XML的新API函數。Jdom是基於樹操作的純Java API,是一套用於解析、創建和實現xml的解決方案。 

下載JDOM包:

官網地址:http://www.jdom.org

下載地址(直接在迅雷裏新建任務即可):http://www.jdom.org/dist/binary/jdom-1.1.1.zip

配置環境變量:將JDOM目錄下的build下的jdom.jar文件,拷貝到JAVA2SKD目錄下的jre/lib/ext目錄下;使用eclipse時,可手動添加到user liberary

基本思路:JDOM讀取XML文件,需先用org.jdom.input.SAXBuilder對象的build()方法創建Document對象,然後用Document類(org.jdom.Document)、Element類(org.jdom.Element)等方法讀取所需的內容。(後詳)

示例:創建一個硬盤上分區磁盤的信息XML文件;讀取其中信息,輸出到控制檯。

基本步驟:

//generateSample.java創建XML

//ReadSample.java 讀取XML中信息

一、JDOM創建XML

  1. //GenerateSample.java   
  2.   
  3. package xml;    
  4.   
  5. import java.io.FileOutputStream;   
  6.   
  7. import java.io.IOException;   
  8.   
  9.     
  10.   
  11. import org.jdom.Document;   
  12.   
  13. import org.jdom.Element;   
  14.   
  15. import org.jdom.JDOMException;   
  16.   
  17. import org.jdom.output.Format;   
  18.   
  19. import org.jdom.output.XMLOutputter;   
  20.   
  21.     
  22.   
  23. public class GenerateSample   
  24.   
  25. {   
  26.   
  27.     //創建XML    
  28.   
  29.     public void BuildXMLDoc() throws IOException, JDOMException    
  30.   
  31.     {    
  32.   
  33.        // 創建根節點 root;    
  34.   
  35.        Element root = new Element("HD");    
  36.   
  37.        // 根節點添加到文檔中;    
  38.   
  39.        Document Doc = new Document(root);    
  40.   
  41.        // 此處 for 循環可替換成 遍歷 數據庫表的結果集操作;    
  42.   
  43.     
  44.   
  45.        // 創建節點 DISK1;    
  46.   
  47.        Element elementsC = new Element("disk");    
  48.   
  49.        // 給DISK1 節點添加屬性 id;    
  50.   
  51.        elementsC.setAttribute("name""C");    
  52.   
  53.        // 給DISK1節點添加子節點並賦值;    
  54.   
  55.        elementsC.addContent(new Element("capacity").setText("8G"));    
  56.   
  57.        elementsC.addContent(new Element("directories").setText("200"));    
  58.   
  59.        elementsC.addContent(new Element("files").setText("5000"));    
  60.   
  61.        // 給父節點disk添加disk子節點;    
  62.   
  63.        root.addContent(elementsC);   
  64.   
  65.           
  66.   
  67.        Element elementsD = new Element("disk");    
  68.   
  69.        // 給DISK1 節點添加屬性 id;    
  70.   
  71.        elementsD.setAttribute("name""D");    
  72.   
  73.        // 給DISK1節點添加子節點並賦值;    
  74.   
  75.        elementsD.addContent(new Element("capacity").setText("20G"));    
  76.   
  77.        elementsD.addContent(new Element("directories").setText("400"));    
  78.   
  79.        elementsD.addContent(new Element("files").setText("1520"));    
  80.   
  81.        // 給父節點disk添加disk子節點;    
  82.   
  83.        root.addContent(elementsD);        
  84.   
  85.           
  86.   
  87.        Element elementsE = new Element("disk");    
  88.   
  89.        // 給DISK1 節點添加屬性 id;    
  90.   
  91.        elementsE.setAttribute("name""E");    
  92.   
  93.        // 給DISK1節點添加子節點並賦值;    
  94.   
  95.        elementsE.addContent(new Element("capacity").setText("20G"));    
  96.   
  97.        elementsE.addContent(new Element("directories").setText("500"));    
  98.   
  99.        elementsE.addContent(new Element("files").setText("10200"));    
  100.   
  101.        // 給父節點disk添加disk子節點;    
  102.   
  103.        root.addContent(elementsE);    
  104.   
  105.        
  106.   
  107.        Element elementsF = new Element("disk");    
  108.   
  109.        // 給DISK1 節點添加屬性 id;    
  110.   
  111.        elementsF.setAttribute("name""F");    
  112.   
  113.        // 給DISK1節點添加子節點並賦值;    
  114.   
  115.        elementsF.addContent(new Element("capacity").setText("30G"));    
  116.   
  117.        elementsF.addContent(new Element("directories").setText("700"));    
  118.   
  119.        elementsF.addContent(new Element("files").setText("30000"));    
  120.   
  121.        // 給父節點disk添加disk子節點;    
  122.   
  123.        root.addContent(elementsF);   
  124.   
  125.           
  126.   
  127.        //定義輸出    
  128.   
  129.        XMLOutputter XMLOut = new XMLOutputter();    
  130.   
  131.        //設置格式    
  132.   
  133.        Format format = Format.getPrettyFormat();    
  134.   
  135.        format.setEncoding("UTF-8"); //設置xml文件的字符爲UTF-8    
  136.   
  137.        format.setIndent(" "); //設置xml文件的縮進爲4個空格    
  138.   
  139.        XMLOut.setFormat(format);    
  140.   
  141.        // 輸出 user.xml 文件;    
  142.   
  143.        XMLOut.output(Doc, new FileOutputStream("F:/sample.xml"));    
  144.   
  145.     }   
  146.   
  147.     public static void main(String[] args)   
  148.   
  149.     {    
  150.   
  151.        try  
  152.   
  153.        {    
  154.   
  155.            GenerateSample j2x = new GenerateSample();    
  156.   
  157.            j2x.BuildXMLDoc(); //創建    
  158.   
  159.            System.out.println("Sample.xml has already generated successfully!");   
  160.   
  161.        }   
  162.   
  163.        catch (Exception e)    
  164.   
  165.        {    
  166.   
  167.            e.printStackTrace();    
  168.   
  169.        }    
  170.   
  171.     }      
  172.   
  173. }   
  174.   

結果:

     產生F:/sample.xml文件

     控制檯輸出結果:

Sample.xml has already generated successfully!

Sample.xml文件中內容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <HD>  
  4.   
  5.  <disk name="C">  
  6.   
  7.   <capacity>8G</capacity>  
  8.   
  9.   <directories>200</directories>  
  10.   
  11.   <files>5000</files>  
  12.   
  13.  </disk>  
  14.   
  15.  <disk name="D">  
  16.   
  17.   <capacity>20G</capacity>  
  18.   
  19.   <directories>400</directories>  
  20.   
  21.   <files>1520</files>  
  22.   
  23.  </disk>  
  24.   
  25.  <disk name="E">  
  26.   
  27.   <capacity>20G</capacity>  
  28.   
  29.   <directories>500</directories>  
  30.   
  31.   <files>10200</files>  
  32.   
  33.  </disk>  
  34.   
  35.  <disk name="F">  
  36.   
  37.   <capacity>30G</capacity>  
  38.   
  39.   <directories>700</directories>  
  40.   
  41.   <files>30000</files>  
  42.   
  43.  </disk>  
  44.   
  45. </HD>  
  46.   

二、

//ReadSapmle.java

  1. package xml;    
  2.   
  3. import java.util.List;    
  4.   
  5. import org.jdom.Document;   
  6.   
  7. import org.jdom.Element;   
  8.   
  9. import org.jdom.input.SAXBuilder;   
  10.   
  11.     
  12.   
  13. public class ReadSample   
  14.   
  15. {   
  16.   
  17.     public static void main(String[] args) throws Exception   
  18.   
  19.     {   
  20.   
  21.        SAXBuilder sb=new SAXBuilder();   
  22.   
  23.        Document doc=sb.build("F:/sample.xml");//構造文檔對象   
  24.   
  25.        Element root=doc.getRootElement();//獲得根元素   
  26.   
  27.        List list=root.getChildren("disk");//取標記爲disk的所有元素   
  28.   
  29.           
  30.   
  31.        for(int i=0;i<list.size();i++)   
  32.   
  33.        {   
  34.   
  35.            Element element=(Element)list.get(i);   
  36.   
  37.            String name=element.getAttributeValue("name");   
  38.   
  39.            String capacity=element.getChildText("capacity");   
  40.   
  41.            String directories=element.getChildText("directories");   
  42.   

    XML是數據領域的Java語言,它使數據獨立於創建它的軟件和該軟件所在的操作系統,就像Java使軟件獨立於操作系統一樣。

    “Jdom makes xml easy said by jason hunter.

    Jdom是用Java語言讀、寫、操作XML的新API函數。Jdom是基於樹操作的純Java API,是一套用於解析、創建和實現xml的解決方案。 

    下載JDOM包:

    官網地址:http://www.jdom.org

    下載地址(直接在迅雷裏新建任務即可):http://www.jdom.org/dist/binary/jdom-1.1.1.zip

    配置環境變量:將JDOM目錄下的build下的jdom.jar文件,拷貝到JAVA2SKD目錄下的jre/lib/ext目錄下;使用eclipse時,可手動添加到user liberary

    基本思路:JDOM讀取XML文件,需先用org.jdom.input.SAXBuilder對象的build()方法創建Document對象,然後用Document類(org.jdom.Document)、Element類(org.jdom.Element)等方法讀取所需的內容。(後詳)

    示例:創建一個硬盤上分區磁盤的信息XML文件;讀取其中信息,輸出到控制檯。

    基本步驟:

    //generateSample.java創建XML

    //ReadSample.java 讀取XML中信息

    一、JDOM創建XML

    1. //GenerateSample.java   
    2.   
    3. package xml;    
    4.   
    5. import java.io.FileOutputStream;   
    6.   
    7. import java.io.IOException;   
    8.   
    9.     
    10.   
    11. import org.jdom.Document;   
    12.   
    13. import org.jdom.Element;   
    14.   
    15. import org.jdom.JDOMException;   
    16.   
    17. import org.jdom.output.Format;   
    18.   
    19. import org.jdom.output.XMLOutputter;   
    20.   
    21.     
    22.   
    23. public class GenerateSample   
    24.   
    25. {   
    26.   
    27.     //創建XML    
    28.   
    29.     public void BuildXMLDoc() throws IOException, JDOMException    
    30.   
    31.     {    
    32.   
    33.        // 創建根節點 root;    
    34.   
    35.        Element root = new Element("HD");    
    36.   
    37.        // 根節點添加到文檔中;    
    38.   
    39.        Document Doc = new Document(root);    
    40.   
    41.        // 此處 for 循環可替換成 遍歷 數據庫表的結果集操作;    
    42.   
    43.     
    44.   
    45.        // 創建節點 DISK1;    
    46.   
    47.        Element elementsC = new Element("disk");    
    48.   
    49.        // 給DISK1 節點添加屬性 id;    
    50.   
    51.        elementsC.setAttribute("name""C");    
    52.   
    53.        // 給DISK1節點添加子節點並賦值;    
    54.   
    55.        elementsC.addContent(new Element("capacity").setText("8G"));    
    56.   
    57.        elementsC.addContent(new Element("directories").setText("200"));    
    58.   
    59.        elementsC.addContent(new Element("files").setText("5000"));    
    60.   
    61.        // 給父節點disk添加disk子節點;    
    62.   
    63.        root.addContent(elementsC);   
    64.   
    65.           
    66.   
    67.        Element elementsD = new Element("disk");    
    68.   
    69.        // 給DISK1 節點添加屬性 id;    
    70.   
    71.        elementsD.setAttribute("name""D");    
    72.   
    73.        // 給DISK1節點添加子節點並賦值;    
    74.   
    75.        elementsD.addContent(new Element("capacity").setText("20G"));    
    76.   
    77.        elementsD.addContent(new Element("directories").setText("400"));    
    78.   
    79.        elementsD.addContent(new Element("files").setText("1520"));    
    80.   
    81.        // 給父節點disk添加disk子節點;    
    82.   
    83.        root.addContent(elementsD);        
    84.   
    85.           
    86.   
    87.        Element elementsE = new Element("disk");    
    88.   
    89.        // 給DISK1 節點添加屬性 id;    
    90.   
    91.        elementsE.setAttribute("name""E");    
    92.   
    93.        // 給DISK1節點添加子節點並賦值;    
    94.   
    95.        elementsE.addContent(new Element("capacity").setText("20G"));    
    96.   
    97.        elementsE.addContent(new Element("directories").setText("500"));    
    98.   
    99.        elementsE.addContent(new Element("files").setText("10200"));    
    100.   
    101.        // 給父節點disk添加disk子節點;    
    102.   
    103.        root.addContent(elementsE);    
    104.   
    105.        
    106.   
    107.        Element elementsF = new Element("disk");    
    108.   
    109.        // 給DISK1 節點添加屬性 id;    
    110.   
    111.        elementsF.setAttribute("name""F");    
    112.   
    113.        // 給DISK1節點添加子節點並賦值;    
    114.   
    115.        elementsF.addContent(new Element("capacity").setText("30G"));    
    116.   
    117.        elementsF.addContent(new Element("directories").setText("700"));    
    118.   
    119.        elementsF.addContent(new Element("files").setText("30000"));    
    120.   
    121.        // 給父節點disk添加disk子節點;    
    122.   
    123.        root.addContent(elementsF);   
    124.   
    125.           
    126.   
    127.        //定義輸出    
    128.   
    129.        XMLOutputter XMLOut = new XMLOutputter();    
    130.   
    131.        //設置格式    
    132.   
    133.        Format format = Format.getPrettyFormat();    
    134.   
    135.        format.setEncoding("UTF-8"); //設置xml文件的字符爲UTF-8    
    136.   
    137.        format.setIndent(" "); //設置xml文件的縮進爲4個空格    
    138.   
    139.        XMLOut.setFormat(format);    
    140.   
    141.        // 輸出 user.xml 文件;    
    142.   
    143.        XMLOut.output(Doc, new FileOutputStream("F:/sample.xml"));    
    144.   
    145.     }   
    146.   
    147.     public static void main(String[] args)   
    148.   
    149.     {    
    150.   
    151.        try  
    152.   
    153.        {    
    154.   
    155.            GenerateSample j2x = new GenerateSample();    
    156.   
    157.            j2x.BuildXMLDoc(); //創建    
    158.   
    159.            System.out.println("Sample.xml has already generated successfully!");   
    160.   
    161.        }   
    162.   
    163.        catch (Exception e)    
    164.   
    165.        {    
    166.   
    167.            e.printStackTrace();    
    168.   
    169.        }    
    170.   
    171.     }      
    172.   
    173. }   
    174.   

    結果:

         產生F:/sample.xml文件

         控制檯輸出結果:

    Sample.xml has already generated successfully!

    Sample.xml文件中內容:

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <HD>  
    4.   
    5.  <disk name="C">  
    6.   
    7.   <capacity>8G</capacity>  
    8.   
    9.   <directories>200</directories>  
    10.   
    11.   <files>5000</files>  
    12.   
    13.  </disk>  
    14.   
    15.  <disk name="D">  
    16.   
    17.   <capacity>20G</capacity>  
    18.   
    19.   <directories>400</directories>  
    20.   
    21.   <files>1520</files>  
    22.   
    23.  </disk>  
    24.   
    25.  <disk name="E">  
    26.   
    27.   <capacity>20G</capacity>  
    28.   
    29.   <directories>500</directories>  
    30.   
    31.   <files>10200</files>  
    32.   
    33.  </disk>  
    34.   
    35.  <disk name="F">  
    36.   
    37.   <capacity>30G</capacity>  
    38.   
    39.   <directories>700</directories>  
    40.   
    41.   <files>30000</files>  
    42.   
    43.  </disk>  
    44.   
    45. </HD>  
    46.   

    二、

    //ReadSapmle.java

    1. package xml;    
    2.   
    3. import java.util.List;    
    4.   
    5. import org.jdom.Document;   
    6.   
    7. import org.jdom.Element;   
    8.   
    9. import org.jdom.input.SAXBuilder;   
    10.   
    11.     
    12.   
    13. public class ReadSample   
    14.   
    15. {   
    16.   
    17.     public static void main(String[] args) throws Exception   
    18.   
    19.     {   
    20.   
    21.        SAXBuilder sb=new SAXBuilder();   
    22.   
    23.        Document doc=sb.build("F:/sample.xml");//構造文檔對象   
    24.   
    25.        Element root=doc.getRootElement();//獲得根元素   
    26.   
    27.        List list=root.getChildren("disk");//取標記爲disk的所有元素   
    28.   
    29.           
    30.   
    31.        for(int i=0;i<list.size();i++)   
    32.   
    33.        {   
    34.   
    35.            Element element=(Element)list.get(i);   
    36.   
    37.            String name=element.getAttributeValue("name");   
    38.   
    39.            String capacity=element.getChildText("capacity");   
    40.   
    41.            String directories=element.getChildText("directories");   
    42.   
    43.            String files=element.getChildText("files");   
    44.   
    45.            System.out.println("磁盤信息:");   
    46.   
    47.            System.out.println("分區盤符:"+name);   
    48.   
    49.            System.out.println("分區容量:"+capacity);   
    50.   
    51.            System.out.println("目錄數:"+directories);   
    52.   
    53.            System.out.println("文件數:"+files);   
    54.   
    55.            System.out.println("---------------------");   
    56.   
    57.        }   
    58.   
    59.     }   
    60.   
    61. }  
  43.            String files=element.getChildText("files");   
  44.   
  45.            System.out.println("磁盤信息:");   
  46.   
  47.            System.out.println("分區盤符:"+name);   
  48.   
  49.            System.out.println("分區容量:"+capacity);   
  50.   
  51.            System.out.println("目錄數:"+directories);   
  52.   
  53.            System.out.println("文件數:"+files);   
  54.   
  55.            System.out.println("---------------------");   
  56.   
  57.        }   
  58.   
  59.     }   
  60.   
  61. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章