现在的位置: 首页 > 综合 > 正文

Jdom使用指南

2013年09月15日 ⁄ 综合 ⁄ 共 3319字 ⁄ 字号 评论关闭
 

xml文档的解析java中有很多种方法,例如使用domsaxjdom等等,相比之下,我觉得还是jdom比较方便。下面介绍一下jdom的基本使用方法,不对之处还请各位网友之交。谢谢!

最新的jdom可以到他的网站:http://www.jdom.org去下载,现在的版本是1.0版,下载之后将得到jdom-1.0.zip文件,解压后进入build文件夹将看到一个名为jdom.jar的包,这个就是jdom的类包了,将它加到你的classpath里就可以使用jdom提供的各种处理xml的类和他们的方法了。应该注意的是在解压后的文件夹里还有一个lib文件夹,里面保存的是使用jdom的环境包,不过我在我的jdk1.4下使用没引用这些包一样好用,不知道是jdk1.4中已经包含了这些东西还是原来我的eclipse已经引用了这些包,呵呵。

好了,书归正传,现在开始介绍jdom包的使用。


   jdom
包的结构包括:

org.jdom                包含了所有的xml文档要素的java

org.jdom.adapters         包含了与dom适配的java

org.jdom.filter            包含了xml文档的过滤器类

org.jdom.input            包含了读取xml文档的类

org.jdom.output           包含了写入xml文档的雷

org.jdom.transform        包含了将jdom xml文档接口转换为其他xml文档接口

org.jdom.xpath            包含了对xml文档xpath操作的类

下面将通过一个例子介绍jdom的常用操作

 

生成xml文档:

下面的类将生成一个xml文档:

/*

 * Created on 2004-10-9

 *

 * 写入xml文件的例子

 */

 

/**

 * @author lnman

 *

 * To change the template for this generated type comment go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

import java.io.*;

import org.jdom.*;

import org.jdom.output.*;

 

public class WriteXML

{

    public void BuildXML() throws Exception

    {

        Element root,student,number,name,age;

               

        root = new Element("student-info"); //生成根元素:student-info  

        student = new Element("student");      //生成元素:student,该元素中将包含元素number,name,age

        number = new Element("number");

        name = new Element("name");

        age = new Element("age");

       

        Document doc = new Document(root);    //将根元素植入文档doc

       

        number.setText("001");

        name.setText("lnman");

        age.setText("24");

        student.addContent(number);

        student.addContent(name);

        student.addContent(age);

        root.addContent(student);

       

        Format format = Format.getCompactFormat();

        format.setEncoding("gb2312");           //设置xml文件的字符为gb2312

        format.setIndent("    ");               //设置xml文件的缩进为4个空格

       

        XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格

        XMLOut.output(doc, new FileOutputStream("studentinfo.xml")); 

       

    }

   

    public static void main(String[] args) throws Exception

    {

        WriteXML w = new WriteXML();

        System.out.println("Now we build an XML document .....");

        w.BuildXML();

        System.out.println("finished!");

    }

 

}

 

生成的xml文档为:

<?xml version="1.0" encoding="gb2312"?>

<student-info>

    <student>

        <number>001</number>

        <name>lnman</name>

        <age>24</age>

    </student>

</student-info>

 

读取xml文档的例子:

/*

 * Created on 2004-10-9

 *

 *jdom读取xml文档的例子

 *

 */

 

/**

 * @author lnman

 *

 * To change the template for this generated type comment go to

 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

 */

import org.jdom.output.*;

import org.jdom.input.*;

import org.jdom.*;

import java.io.*;

import java.util.*;

public class ReadXML

{

   

    public static void main(String[] args) throws Exception

    {

       

       

        SAXBuilder builder = new SAXBuilder();

        Document read_doc = builder.build("studentinfo.xml");

        Element stu = read_doc.getRootElement();

        List list = stu.getChildren("student");

        for(int i = 0;i < list.size();i++)

        {

            Element e = (Element)list.get(i);

            String str_number = e.getChildText("number");

            String str_name = e.getChildText("name");

            String str_age = e.getChildText("age");

            System.out.println("---------STUDENT--------------");

            System.out.println("NUMBER:" + str_number);

            System.out.println("NAME:" + str_name);

           

抱歉!评论已关闭.