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. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值