首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> XML >> WebServices >> 正文

如何使用JDOM对XML文件进行操作


    程序首先初始化一个SAXBuilder对象,通过其build()方法构建Document对象。注意:JDOM本身没有对XML文件的分析器parser,它是利用JAXP中的parser进行XML分析的。然后通过getRootElement()获得顶层的元素root(XML文件中book标签),继而通过getChild()获取各元素,然后通过getText取得文本内容。getChildren()得到的是List类型的对象,通过Iterator类对其进行迭代。
程序运行的结果如下:
BookTitle: Java and XML
ChapterTitle: Introduction
TopicName: XML Matters
TopicName: What's Important
TopicName: The Essentials
TopicName: What's Next?
ChapterTitle: Nuts and Bolts
TopicName: The Basics
TopicName: Constraints
TopicName: Transformations
TopicName: And More...
TopicName: What's Next?

经常,我们需要把应用程序里的配置数据进行输出到文件中,下面举例说明如何利用JDOM将配置输出到XML文件中。
下面的例子先把应用程序中要使用到的配置从一个配置文件中读入,然后输出到XML文件中。
配置文件名为:conf.properties,内容如下:
BookTitle = Java and XML
ChapterTitle = Introduction
TopicName = XML Matters

程序名为:XMLOutputterTest.java,内容如下:
----------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class XMLOutputterTest {

    public static void main(String[] args) {
        try {
            FileInputStream inputstream = new FileInputStream("conf.properties");
            Properties prop = new Properties();
            prop.load(inputstream);
            Enumeration emu = prop.propertyNames();

            Element root = new Element("properties");
            root.addContent("\n");
            Document doc = new Document(root);

            while (emu.hasMoreElements()) {
                String propertyName = (String) emu.nextElement();
                String propertyValue = prop.getProperty(propertyName);
                Element element = new Element(propertyName);
                element.setText(propertyValue);
                root.addContent(element);
                root.addContent("\n");
            }

            XMLOutputter outputter = new XMLOutputter();
            FileOutputStream fileoutput = new FileOutputStream("output.xml");
            outputter.output(doc, fileoutput);
        } catch (Exception ex) {
        }
    }
}
----------------------------------------------------------
    程序先读入conf.properties文件,通过propertyNames()得出Enumeration迭代对象,进而获得propertyName并得到propertyValue,创建Element类型root对象,并以此创建Document类对象,然后通过addContent()添加到root对象中。最后创建XMLOutputter类对象,通过output()输出到XML文件中。

以上2个程序均在JDK 1.4.2的环境下测试通过,请读者到附件中下载源代码文件。

如果读者对以上的内容有任何疑问,可以和我联系,qianh@cntmi.com 版权所有,严禁转载

参考资料:
1、《Java_and_XML_2nd_Edition》written by Brett McLaughlin, Published by O'Reilly, Second Edition September 2001
2、JDOM主页:http://www.jdom.org/

上一页  [1] [2] 

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航