使用JDOM操作XML文件

使用JDOM操作XML文件

分类: Xml   388人阅读  评论(0)  收藏  举报

XML由于其可移植性,已经成为应用开发中必不可少的环节。我们经常会把应用程序的一些配置文件(属性文件)写成XML的格式(当然,也可以用property文件而不用XML文件),应用程序通过XML的访问类来对其进行操作。对XML进行操作可以通过若干种方法,如:SAX, DOM, JDOM, JAXP等,JDOM由于其比较简单实用而被开发人员普遍使用。
本文主要分两部分,第一部分介绍如何把XML文件中的配置读入应用程序中,第二部分介绍如何使用JDOM将配置输出到XML文件中。

以下是一段XML配置文件,文件名为contents.xml

[java]  view plain copy
  1. <?xml version="1.0"?>  
  2. <book>  
  3.     <title>Java and XML</title>  
  4.     <contents>  
  5.         <chapter title="Introduction">  
  6.             <topic>XML Matters</topic>  
  7.             <topic>What's Important</topic>  
  8.             <topic>The Essentials</topic>  
  9.             <topic>What's Next?</topic>  
  10.         </chapter>  
  11.         <chapter title="Nuts and Bolts">  
  12.             <topic>The Basics</topic>  
  13.             <topic>Constraints</topic>  
  14.             <topic>Transformations</topic>  
  15.             <topic>And More...</topic>  
  16.             <topic>What's Next?</topic>  
  17.         </chapter>  
  18.     </contents>  
  19. </book>  
 下面的程序通过使用JDOM中SAXBuilder类对contents.xml进行访问操作,把各个元素显示在输出console上,程序名为:SAXBuilderTest.java

[java]  view plain copy
  1. import java.io.File;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4. import org.jdom.Document;  
  5. import org.jdom.Element;  
  6. import org.jdom.input.SAXBuilder;  
  7. public class SAXBuilderTest {  
  8.     private static String titlename;  
  9.     private String chapter;  
  10.     private String topic;  
  11.     public static void main(String[] args) {  
  12.         try {  
  13.             SAXBuilder builder = new SAXBuilder();  
  14.             Document document = builder.build(new File("contents.xml"));  
  15.             Element root = document. getRootElement_r();  
  16.             Element title = root. getChild_r("title");  
  17.             titlename = title. getText_r();  
  18.             System.out.println("BookTitle: " + titlename);  
  19.             Element contents = root. getChild_r("contents");  
  20.             List chapters = contents. getChildren_r("chapter");  
  21.             Iterator it = chapters.iterator();  
  22.             while (it.hasNext()) {  
  23.                 Element chapter = (Element) it.next();  
  24.                 String chaptertitle = chapter. getAttributeValue_r("title");  
  25.                 System.out.println("ChapterTitle: " + chaptertitle);  
  26.                 List topics = chapter. getChildren_r("topic");  
  27.                 Iterator iterator = topics.iterator();  
  28.                 while (iterator.hasNext()) {  
  29.                     Element topic = (Element) iterator.next();  
  30.                     String topicname = topic. getText_r();  
  31.                     System.out.println("TopicName: " + topicname);  
  32.                 }  
  33.             }  
  34.         } catch (Exception ex) {  
  35.         }  
  36.     }  
  37. }  
 程序首先初始化一个SAXBuilder对象,通过其build()方法构建Document对象。注意:JDOM本身没有对XML文件的分析器parser,它是利用JAXP中的parser进行XML分析的。

然后通过 getRootElement_r()获得顶层的元素root(XML文件中book标签),继而通过 getChild_r()获取各元素,然后通过getText取得文本内容。 getChildren_r()得到的是List类型的对象,通过Iterator类对其进行迭代。程序运行的结果如下:

[c-sharp]  view plain copy
  1. BookTitle: Java and XML  
  2. ChapterTitle: Introduction  
  3. TopicName: XML Matters  
  4. TopicName: What's Important  
  5. TopicName: The Essentials  
  6. TopicName: What's Next?  
  7. ChapterTitle: Nuts and Bolts  
  8. TopicName: The Basics  
  9. TopicName: Constraints  
  10. TopicName: Transformations  
  11. TopicName: And More...  
  12. TopicName: What's Next?  
 经常,我们需要把应用程序里的配置数据进行输出到文件中,下面举例说明如何利用JDOM将配置输出到XML文件中。

下面的例子先把应用程序中要使用到的配置从一个配置文件中读入,然后输出到XML文件中。
配置文件名为:conf.properties,内容如下

[c-sharp]  view plain copy
  1. BookTitle = Java and XML  
  2. ChapterTitle = Introduction  
  3. TopicName = XML Matters  
 程序名为:XMLOutputterTest.java

[java]  view plain copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.util.Enumeration;  
  4. import java.util.Properties;  
  5. import org.jdom.Document;  
  6. import org.jdom.Element;  
  7. import org.jdom.output.XMLOutputter;  
  8. public class XMLOutputterTest {  
  9.     public static void main(String[] args) {  
  10.         try {  
  11.             FileInputStream inputstream = new FileInputStream("conf.properties");  
  12.             Properties prop = new Properties();  
  13.             prop.load(inputstream);  
  14.             Enumeration emu = prop.propertyNames();  
  15.             Element root = new Element("properties");  
  16.             root.addContent("/n");  
  17.             Document doc = new Document(root);  
  18.             while (emu.hasMoreElements()) {  
  19.                 String propertyName = (String) emu.nextElement();  
  20.                 String propertyValue = prop. getProperty_r(propertyName);  
  21.                 Element element = new Element(propertyName);  
  22.                 element.setText(propertyValue);  
  23.                 root.addContent(element);  
  24.                 root.addContent("/n");  
  25.             }  
  26.             XMLOutputter outputter = new XMLOutputter();  
  27.             FileOutputStream fileoutput = new FileOutputStream("output.xml");  
  28.             outputter.output(doc, fileoutput);  
  29.         } catch (Exception ex) {  
  30.         }  
  31.     }  
  32. }  
 程序先读入conf.properties文件,通过propertyNames()得出Enumeration迭代对象,进而获得propertyName并得到propertyValue,创建Element类型root对象,并以此创建Document类对象,然后通过addContent()添加到root对象中。最后创建XMLOutputter类对象,通过output()输出到XML文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值