JDOM的简单应用

(一)、XML文档创建
我们由零开始利用JDOM生成一个XML文档。最后的结果(样本文档)看起来象这样:
kingwong
87654321
1.以 MyInfo 为根元素创建文档
Element rootElement = new Element("MyInfo");//所有的XML元素都是 Element 的实例。根元素也不例外:)
Document myDocument = new Document(rootElement);//以根元素作为参数创建Document对象。一个Document只有一个根,即root元素。
2.给根元素添加属性
Attribute rootAttri = new Attribute("comment","introduce myself");//创建名为 commnet,值为 introduce myself 的属性。
rootElement.setAttribute(rootAttri);//将刚创建的属性添加到根元素。
这两行代码你也可以合成一行来写,象这样:
rootElement.setAttribute(new Attribute("comment","introduce myself"));
或者
rootElement.setAttribute("comment","introduce myself");
3.添加元素和子元素
JDOM里子元素是作为 content(内容)添加到父元素里面去的,所谓content就是类似上面样本文档中之间的东东,即kingwong。啰嗦了点是吧:)
Element nameElement = new Element("name");//创建 name 元素
nameElement.addContent("kingwong");//将 kingwong作为content添加到name元素
rootElement.addContent(nameElement);//将name元素作为content添加到根元素
这三行你也可以合为一句,象这样:
rootElement.addContent((Content)(new Element("name").addContent("kingwong")));//因为addContent(Content child)方法返回的是一个Parent接口,而Element类同时继承了Content类和实现了Parent接口,所以我们把它造型成 Content。
我们用同样的方法添加带属性的子元素
rootElement.addContent(new Element("sex").setAttribute("value","male"));//注意这里不需要转型,因为 addAttribute(String name,String value)返回值就是一个 Element。
同样的,我们添加元素到根元素下,用法上一样,只是稍微复杂了一些:
rootElement.addContent((Content)(new Element("contact").addContent((Content)(new Element("telephone").addContent("87654321")))));
如果你对这种简写形式还不太习惯,你完全可以分步来做,就象本节刚开始的时候一样。事实上如果层次比较多,写成分步的形式更清晰些,也不容易出错。
4.删除子元素
这个操作比较简单:
rootElement.removeChild("sex");//该方法返回一个布尔值
到目前为止,我们学习了一下JDOM文档生成操作。上面建立了一个样本文档,可是我们怎么知道对不对呢?因此需要输出来看一下。我们将JDOM生成的文档输出到控制台,使用 JDOM 的 XMLOutputter 类。
5. 将 JDOM 转化为 XML 文本
XMLOutputter xmlOut = new XMLOutputter(" ",true);
try {
xmlOut.output(myDocument,System.out);
} catch (IOException e) {
e.printStackTrace();
}
XMLOutputter 有几个格式选项。这里我们已指定希望子元素从父元素缩进两个空格,并且希望元素间有空行。
new XMLOutputter(java.lang.String indent, boolean newlines)这个方法在最新版本中已经不建议使用。JDOM有一个专门的用来定义格式化输出的类:org.jdom.output.Format,如果你没有特殊的要求,有时候使用里面的几个静态方法(应该可以说是预定义格式)如 getPrettyFormat()就可以了。我们把上面的输出格式稍微改一下,就象这样:
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
6.将JDOM文档转化为其他形式
XMLOutputter 还可输出到 Writer 或 OutputStream。为了输出JDOM文档到一个文本文件,我们可以这样做:
FileWriter writer = new FileWriter("/some/directory/myFile.xml");
outputter.output(myDocument, writer);
writer.close();
XMLOutputter 还可输出到字符串,以便程序后面进行再处理:
Strng outString = xmlOut.outputString(myDocument);
当然,在输出的时候你不一定要输出所有的整个文档,你可以选择元素进行输出:
xmlOut.output(rootElement.getChild("name"),System.out);
一句话,JDOM非常灵活方便!如果你想进一步研究JDOM,请到官方网站去看一看:http://www.jdom.org
本节示例源码:
package com.cyberobject.study;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* @author kingwong
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestJDOM {
public static void main(String[] args)
{
Element rootElement = new Element("MyInfo");
Document myDocument = new Document(rootElement);
// Attribute rootAttri = new Attribute("comment","introduce myself");
// rootElement.setAttribute(rootAttri);
rootElement.setAttribute("comment","introduce myself");
//rootElement.setAttribute(new Attribute("comment","introduce myself"));
// Element sexElement = new Element("sex");
// rootElement.addContent(sexElement);
// Element nameElement = new Element("name");
// nameElement.addContent("kingwong");
// rootElement.addContent(nameElement);
rootElement.addContent((Content)(new Element("name").addContent("kingwong")));
rootElement.addContent(new Element("sex").setAttribute("value","male"));
rootElement.addContent((Content)(new Element("contract").addContent((Content)(new Element("telephone").addContent("87654321")))));
rootElement.removeChild("sex");
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
try {
xmlOut.output(myDocument,System.out);
//xmlOut.output(rootElement.getChild("name"),System.out);
//String outString = xmlOut.outputString(myDocument);
} catch (IOException e) {
e.printStackTrace();
}
}
}


posted on 2012-06-12 15:36  niklausxu  阅读(191)  评论(0编辑  收藏  举报

导航