Dom4j
使用簡介
作者:冰云
icecloud(AT)sina.com
時間:
2003.12.15
?
版權聲明:
本文由冰云完成,首發于
CSDN
,未經許可,不得使用于任何商業用途。
文中代碼部分引用自
DOM4J
文檔。
歡迎轉載,但請保持文章及版權聲明完整。
如需聯絡請發郵件:
icecloud(AT)sina.com
?
|
??? DOM4J
是
dom4j.org
出品的一個開源
XML
解析包,它的網站中這樣定義:
Dom4j
is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.
Dom4j
是一個易用的、開源的庫,用于
XML
,
XPath
和
XSLT
。它應用于
Java
平臺,采用了
Java
集合框架并完全支持
DOM
,
SAX
和
JAXP
。
DOM4J
使用起來非常簡單。只要你了解基本的
XML-DOM
模型,就能使用。然而他自己帶的指南只有短短一頁(
html
),不過說的到挺全。國內的中文資料很少。因而俺寫這個短小的教程方便大家使用,這篇文章僅談及基本的用法,如需深入的使用,請
……
自己摸索或查找別的資料。
之前看過
IBM developer
社區的文章(參見附錄),提到一些
XML
解析包的性能比較,其中
DOM4J
的性能非常出色,在多項測試中名列前茅。(事實上
DOM4J
的官方文檔中也引用了這個比較)所以這次的項目中我采用了
DOM4J
作為
XML
解析工具。
在國內比較流行的是使用
JDOM
作為解析器,兩者各擅其長,但
DOM4J
最大的特色是使用大量的接口,這也是它被認為比
JDOM
靈活的主要原因。大師不是說過么,
“
面向接口編程
”
。目前使用
DOM4J
的已經越來越多。如果你善于使用
JDOM
,不妨繼續用下去,只看看本篇文章作為了解與比較,如果你正要采用一種解析器,不如就用
DOM4J
吧。
它的主要接口都在
org.dom4j
這個包里定義:
看名字大致就知道它們的涵義如何了。
要想弄懂這套接口,關鍵的是要明白接口的繼承關系:
一目了然,很多事情都清楚了。大部分都是由
Node
繼承來的。知道這些關系,將來寫程序就不會出現
ClassCastException
了。
下面給出一些例子(部分摘自
DOM4J
自帶的文檔),簡單說一下如何使用。
1.?????????????
讀取并解析
XML
文檔:
讀寫
XML
文檔主要依賴于
org.dom4j.io
包,其中提供
DOMReader
和
SAXReader
兩類不同方式,而調用方式是一樣的。這就是依靠接口的好處。
?
??? //
從文件讀取
XML
,輸入文件名,返回
XML
文檔
??? publicDocumentread(StringfileName)throwsMalformedURLException,DocumentException{
?????? SAXReaderreader=newSAXReader();
?????? Documentdocument=reader.read(newFile(fileName));
?????? returndocument;
??? }
?
|
其中,
reader
的
read
方法是重載的,可以從
InputStream, File, Url
等多種不同的源來讀取。得到的
Document
對象就帶表了整個
XML
。
根據本人自己的經驗,讀取的字符編碼是按照
XML
文件頭定義的編碼來轉換。如果遇到亂碼問題,注意要把各處的編碼名稱保持一致即可。
2.???
取得
Root
節點
讀取后的第二步,就是得到
Root
節點。熟悉
XML
的人都知道,一切
XML
分析都是從
Root
元素開始的。
?
?
public
Element
getRootElement(Document
doc){
?????? returndoc.getRootElement();
??? }
?
|
3.???
遍歷
XML
樹
DOM4J
提供至少
3
種遍歷節點的方法:
1)
枚舉
(Iterator)
?
??? //
枚舉所有子節點
??? for
(
Iterator
i
=
root.elementIterator();
i.hasNext();
)
{
?????? Elementelement=(Element)i.next();
?????? // do something
??? }
??? //
枚舉名稱為
foo
的節點
??? for
(
Iterator
i
=
root.elementIterator(
foo
);
i.hasNext();)
{
?????? Elementfoo=(Element)i.next();
?????? // do something
??? }
??? //
枚舉屬性
??? for
(
Iterator
i
=
root.attributeIterator();
i.hasNext();
)
{
?????? Attributeattribute=(Attribute)i.next();
?????? // do something
???
}
|
2)
遞歸
遞歸也可以采用
Iterator
作為枚舉手段,但文檔中提供了另外的做法
?
??? publicvoidtreeWalk(){
?????? treeWalk(getRootElement());
??? }
??? publicvoidtreeWalk(Elementelement){
?????? for(inti=0,size=element.nodeCount();i<size;i++)??? {
?????????? Nodenode=element.node(i);
?????????? if(nodeinstanceofElement){
????????????? treeWalk((Element)node);
?????????? }else{// do something....
?????????? }
?????? }
}
?
|
3) Visitor
模式
最令人興奮的是
DOM4J
對
Visitor
的支持,這樣可以大大縮減代碼量,并且清楚易懂。了解設計模式的人都知道,
Visitor
是
GOF
設計模式之一。其主要原理就是兩種類互相保有對方的引用,并且一種作為
Visitor
去訪問許多
Visitable
。我們來看
DOM4J
中的
Visitor
模式
(
快速文檔中沒有提供
)
只需要自定一個類實現
Visitor
接口即可。
?
????
? publicclassMyVisitorextendsVisitorSupport{
?????? ??? publicvoidvisit(Elementelement){
?????????? ??? System.out.println(element.getName());
?????? ??? }
???
???
?? public
void
visit(Attribute
attr){
?????????? ??? System.out.println(attr.getName());
?????? ??? }
???????
}
?
???????
調用:
? root.accept(new MyVisitor())
|
??? Visitor
接口提供多種
Visit()
的重載,根據
XML
不同的對象,將采用不同的方式來訪問。上面是給出的
Element
和
Attribute
的簡單實現,一般比較常用的就是這兩個。
VisitorSupport
是
DOM4J
提供的默認適配器,
Visitor
接口的
Default Adapter
模式,這個模式給出了各種
visit(*)
的空實現,以便簡化代碼。
???
注意,這個
Visitor
是自動遍歷所有子節點的。如果是
root.accept(MyVisitor)
,將遍歷子節點。我第一次用的時候,認為是需要自己遍歷,便在遞歸中調用
Visitor
,結果可想而知。
4. XPath
支持
??? DOM4J
對
XPath
有良好的支持,如訪問一個節點,可直接用
XPath
選擇。
?
?? publicvoidbar(Documentdocument){
?????? ?Listlist=document.selectNodes(//foo/bar);
?????? ?Nodenode=document.selectSingleNode(//foo/bar/author);
?????? ?Stringname=node.valueOf(@name);
??? ?}
?
|
???
例如,如果你想查找
XHTML
文檔中所有的超鏈接,下面的代碼可以實現:
?
??? publicvoidfindLinks(Documentdocument)throwsDocumentException{
?????? ?Listlist=document.selectNodes(//a/@href);
?????? ?for(Iteratoriter=list.iterator();iter.hasNext();){
?????????? ?Attributeattribute=(Attribute)iter.next();
?????????? ?Stringurl=attribute.getValue();
?????? ?}
??? ?}
?
|
5.
字符串與
XML
的轉換
有時候經常要用到字符串轉換為
XML
或反之,
?
???
// XML
轉字符串
Document
document
=
...;
??? Stringtext=document.asXML();
//
字符串轉
XML
??? Stringtext=<person> <name>James</name> </person>;
??? Documentdocument=DocumentHelper.parseText(text);
?
|
6
用
XSLT
轉換
XML
?
?? publicDocumentstyleDocument(
?????? Documentdocument,
?????? Stringstylesheet
??? )throwsException{
??? // load the transformer using JAXP
??? TransformerFactoryfactory=TransformerFactory.newInstance();
??? Transformertransformer=factory.newTransformer(
?????? newStreamSource(stylesheet)
??? );
??? // now lets style the given document
??? DocumentSourcesource=newDocumentSource(document);
??? DocumentResultresult=newDocumentResult();
??? transformer.transform(source,result);
??? // return the transformed document
??? DocumenttransformedDoc=result.getDocument();
??? returntransformedDoc;
}
?
|
7.
創建
XML
?
一般創建
XML
是寫文件前的工作,這就像
StringBuffer
一樣容易。
?
??? publicDocumentcreateDocument(){
?????? Documentdocument=DocumentHelper.createDocument();
?????? Elementroot=document.addElement(root);
?????? Elementauthor1=
?????????? root
????????????? .addElement(author)
????????????? .addAttribute(name,James)
????????????? .addAttribute(location,UK)
????????????? .addText(James Strachan);
?????? Elementauthor2=
?????????? root
????????????? .addElement(author)
????????????? .addAttribute(name,Bob)
????????????? .addAttribute(location,US)
????????????? .addText(Bob McWhirter);
?????? returndocument;
??? }
?
|
8.
文件輸出
???
一個簡單的輸出方法是將一個
Document
或任何的
Node
通過
write
方法輸出
?
??? FileWriter
out
=
new
FileWriter(
foo.xml
);
??? document.write(out);
?
|
?
如果你想改變輸出的格式,比如美化輸出或縮減格式,可以用
XMLWriter
類
?
??? publicvoidwrite(Documentdocument)throwsIOException{
?????? //
指定文件
?????? XMLWriterwriter=newXMLWriter(
??? ?????? newFileWriter(output.xml)
?????? );
?????? writer.write(document);
?????? writer.close();
?????? //
美化格式
?????? OutputFormatformat=OutputFormat.createPrettyPrint();
?????? writer=newXMLWriter(System.out,format);
?????? writer.write(document);
?????? //
縮減格式
?????? format=OutputFormat.createCompactFormat();
?????? writer=newXMLWriter(System.out,format);
?????? writer.write(document);
??? }
?
|
如何,
DOM4J
夠簡單吧,當然,還有一些復雜的應用沒有提到,如
ElementHandler
等。如果你動心了,那就一起來用
DOM4J.
DOM4J
官方網站:
(
我老連不上
)
http://www.dom4j.org
DOM4J
下載
(SourceForge)
,最新版本為
1.4
http://sourceforge.net/projects/dom4j
參考資料:
DOM4J
文檔
Java
中的
XML
:
文檔模型,第一部分:性能
http://www-900.ibm.com/developerWorks/cn/xml/x-injava/index.shtml
Java
中的
XML
:
Java
文檔模型的用法
http://www-900.ibm.com/developerWorks/cn/xml/x-injava2/index.shtml
Java XML API
漫談
by robbin
http://www.hibernate.org.cn:8000/137.html