一、
JDOM
簡(jiǎn)介
JDOM
是一個(gè)開(kāi)源項(xiàng)目,它基于樹(shù)型結(jié)構(gòu),利用純
Java
的技術(shù)對(duì)
XML
文檔實(shí)現(xiàn)解析、生成、序列化以及多種操作。
JDOM
直接為
Java
編程服務(wù)。它利用更為強(qiáng)有力的
Java
語(yǔ)言的諸多特性(方法重載、集合概念以及映射),把
SAX
和
DOM
的功能有效地結(jié)合起來(lái)。
在使用設(shè)計(jì)上盡可能地隱藏原來(lái)使用
XML
過(guò)程中的復(fù)雜性。利用
JDOM
處理
XML
文檔將是一件輕松、簡(jiǎn)單的事。
JDOM
在
2000
年的春天被
Brett McLaughlin
和
Jason Hunter
開(kāi)發(fā)出來(lái),以彌補(bǔ)
DOM
及
SAX
在實(shí)際應(yīng)用當(dāng)中的不足之處。
這些不足之處主要在于
SAX
沒(méi)有文檔修改、隨機(jī)訪問(wèn)以及輸出的功能,而對(duì)于
DOM
來(lái)說(shuō),
Java
程序員在使用時(shí)來(lái)用起來(lái)總覺(jué)得不太方便。
DOM
的缺點(diǎn)主要是來(lái)自于由于
Dom
是一個(gè)接口定義語(yǔ)言(
IDL
)
,
它的任務(wù)是在不同語(yǔ)言實(shí)現(xiàn)中的一個(gè)最低的通用標(biāo)準(zhǔn),并不是為
Java
特別設(shè)計(jì)的。
JDOM
的最新版本為
JDOM Beta 9
。最近
JDOM
被收錄到
JSR-102
內(nèi),這標(biāo)志著
JDOM
成為了
Java
平臺(tái)組成的一部分。
二、
JDOM
包概覽
JDOM
是由以下幾個(gè)包組成的
org.JDOM
org.JDOM.input
org.JDOM.output
org.JDOM.adapters
org.JDOM.transform
三、
JDOM
類(lèi)說(shuō)明
org.JDOM
這個(gè)包里的類(lèi)是你解析
xml
文件后所要用到的所有數(shù)據(jù)類(lèi)型。
Attribute
CDATA
Coment
DocType
Document
Element
EntityRef
Namespace
ProscessingInstruction
Text
org.JDOM.transform
在涉及
xslt
格式轉(zhuǎn)換時(shí)應(yīng)使用下面的
2
個(gè)類(lèi)
JDOMSource
JDOMResult
org.JDOM.input
輸入類(lèi),一般用于文檔的創(chuàng)建工作
SAXBuilder
DOMBuilder
ResultSetBuilder
org.JDOM.output
輸出類(lèi),用于文檔轉(zhuǎn)換輸出
XMLOutputter
SAXOutputter
DomOutputter
JTreeOutputter
使用前注意事項(xiàng):
1.JDOM
對(duì)于
JAXP
以及
TRax
的支持
JDOM
支持
JAXP1.1
:你可以在程序中使用任何的
parser
工具類(lèi)
,
默認(rèn)情況下是
JAXP
的
parser
。
制定特別的
parser
可用如下形式
SAXBuilder parser
= new SAXBuilder("org.apache.crimson.parser.XMLReaderImpl");
Document doc = parser.build("http://www.cafeconleche.org/");
// work with the document...
JDOM
也支持
TRaX
:
XSLT
可通過(guò)
JDOMSource
以及
JDOMResult
類(lèi)來(lái)轉(zhuǎn)換(參見(jiàn)以后章節(jié))
2.
注意在
JDOM
里文檔(
Document
)類(lèi)由
org.JDOM.Document
來(lái)表示。這要與
org.w3c.dom
中的
Document
區(qū)別開(kāi),這
2
種格式如何轉(zhuǎn)換在后面會(huì)說(shuō)明。
以下如無(wú)特指均指
JDOM
里的
Document
。
四、
JDOM
主要使用方法
1.Ducument
類(lèi)
(1)Document
的操作方法:
Element root = new Element("GREETING");
Document doc = new Document(root);
root.setText("Hello JDOM!");
或者簡(jiǎn)單的使用
Document doc = new Document(new Element("GREETING").setText("Hello JDOM!t"));
這點(diǎn)和
DOM
不同。
Dom
則需要更為復(fù)雜的代碼,如下:
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root =doc.createElement("root");
Text text = doc.createText("This is the root");
root.appendChild(text);
doc.appendChild(root);
注意事項(xiàng):
JDOM
不允許同一個(gè)節(jié)點(diǎn)同時(shí)被
2
個(gè)或多個(gè)文檔相關(guān)聯(lián),要在第
2
個(gè)文檔中使用原來(lái)老文檔中的節(jié)點(diǎn)的話。首先需要使用
detach()
把這個(gè)節(jié)點(diǎn)分開(kāi)來(lái)。
(2)
從文件、流、系統(tǒng)
ID
、
URL
得到
Document
對(duì)象:
DOMBuilder builder = new DOMBuilder();
Document doc = builder.build(new File("jdom_test.xml"));
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);
在新版本中
DOMBuilder
已經(jīng)
Deprecated
掉
DOMBuilder.builder(url)
,用
SAX
效率會(huì)比較快。
這里舉一個(gè)小例子,為了簡(jiǎn)單起見(jiàn),使用
String
對(duì)象直接作為
xml
數(shù)據(jù)源:
public jdomTest() {
String textXml = null;
textXml = "<note>";
textXml = textXml +
"<to>aaa</to><from>bbb</from><heading>ccc</heading><body>ddd</body>";
textXml = textXml + "</note>";
SAXBuilder builder = new SAXBuilder();
Document doc = null;
Reader in= new StringReader(textXml);
try {
doc = builder.build(in);
Element root = doc.getRootElement();
List ls = root.getChildren();//
注意此處取出的是
root
節(jié)點(diǎn)下面的一層的
Element
集合
for (Iterator iter = ls.iterator(); iter.hasNext(); ) {
Element el = (Element) iter.next();
if(el.getName().equals("to")){
System.out.println(el.getText());
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (JDOMException ex) {
ex.printStackTrace();
}
}
很簡(jiǎn)單把。
(3)DOM
的
document
和
JDOM
的
Document
之間的相互轉(zhuǎn)換使用方法,簡(jiǎn)單!
DOMBuilder builder = new DOMBuilder();
org.jdom.Document jdomDocument = builder.build(domDocument);
// work with the JDOM document…
DOMOutputter converter = new DOMOutputter();
org.w3c.dom.Document domDocument = converter.output(jdomDocument);
// work with the DOM document…
2.XML
文檔輸出
XMLOutPutter
類(lèi):
JDOM
的輸出非常靈活
,
支持很多種
io
格式以及風(fēng)格的輸出
Document doc = new Document(...);
XMLOutputter outp = new XMLOutputter();
// Raw output
outp.output(doc, fileOutputStream);
// Compressed output
outp.setTextTrim(true);
outp.output(doc, socket.getOutputStream());
// Pretty output
outp.setIndent(" ");
outp.setNewlines(true);
outp.output(doc, System.out);
......
詳細(xì)請(qǐng)參閱最新的
JDOM API
手冊(cè)
3.Element
類(lèi):
(1)
瀏覽
Element
樹(shù)
//
獲得根元素
element
Element root = doc.getRootElement();
//
獲得所有子元素的一個(gè)
list
List allChildren = root.getChildren();
//
獲得指定名稱(chēng)子元素的
list
List namedChildren = root.getChildren("name");
//
獲得指定名稱(chēng)的第一個(gè)子元素
Element child = root.getChild("name");
(這里的
List
是
Java.util.List
)
JDOM
給了我們很多很靈活的使用方法來(lái)管理子元素
List allChildren = root.getChildren();
//
刪除第四個(gè)子元素
allChildren.remove(3);
//
刪除叫
“jack”
的子元素
allChildren.removeAll(root.getChildren("jack"));
root.removeChildren("jack"); //
便捷寫(xiě)法
//
加入
allChildren.add(new Element("jane"));
root.addContent(new Element("jane")); //
便捷寫(xiě)法
allChildren.add(0, new Element("first"));
(2)
移動(dòng)
Elements:
在
JDOM
里很簡(jiǎn)單
Element movable = new Element("movable");
parent1.addContent(movable); // place
parent1.removeContent(movable); // remove
parent2.addContent(movable); // add
在
Dom
里
Element movable = doc1.createElement("movable");
parent1.appendChild(movable); // place
parent1.removeChild(movable); // remove
parent2.appendChild(movable); //
出錯(cuò)
!
補(bǔ)充:
糾錯(cuò)性
JDOM
的
Element
構(gòu)造函數(shù)(以及它的其他函數(shù))會(huì)檢查
element
是否合法。
而它的
add/remove
方法會(huì)檢查樹(shù)結(jié)構(gòu),檢查內(nèi)容如下:
1.
在任何樹(shù)中是否有回環(huán)節(jié)點(diǎn)
2.
是否只有一個(gè)根節(jié)點(diǎn)
3.
是否有一致的命名空間(
Namespaces
)
(3)Element
的
text
內(nèi)容讀取
<description>
A cool demo
</description>
// The text is directly available
// Returns "\n A cool demo\n"
String desc = element.getText();
// There's a convenient shortcut
// Returns "A cool demo"
String desc = element.getTextTrim();
(4)Elment
內(nèi)容修改
element.setText("A new description");
3.
可正確解釋特殊字符
element.setText("<xml> content");
4.CDATA
的數(shù)據(jù)寫(xiě)入、讀出
element.addContent(new CDATA("<xml> content"));
String noDifference = element.getText();
混合內(nèi)容
element
可能包含很多種內(nèi)容,比如說(shuō)
<table>
<!-- Some comment -->
Some text
<tr>Some child element</tr>
</table>
取
table
的子元素
tr
String text = table.getTextTrim();
Element tr = table.getChild("tr");
也可使用另外一個(gè)比較簡(jiǎn)單的方法
List mixedCo = table.getContent();
Iterator itr = mixedCo.iterator();
while (itr.hasNext()) {
Object o = i.next();
if (o instanceof Comment) {
...
}
//
這里可以寫(xiě)成
Comment, Element, Text, CDATA,ProcessingInstruction,
或者是
EntityRef
的類(lèi)型
}
//
現(xiàn)在移除
Comment,
注意這里游標(biāo)應(yīng)為
1
。這是由于回車(chē)鍵也被解析成
Text
類(lèi)的緣故
,
所以
Comment
項(xiàng)應(yīng)為
1
。
mixedCo.remove(1);
4.Attribute
類(lèi)
<table width="100%" border="0"> </table>
//
獲得
attribute
String width = table.getAttributeValue("width");
int border = table.getAttribute("width").getIntValue();
//
設(shè)置
attribute
table.setAttribute("vspace", "0");
//
刪除一個(gè)或全部
attribute
table.removeAttribute("vspace");
table.getAttributes().clear();
5.
處理指令
(Processing Instructions)
操作
一個(gè)
Pls
的例子
<?br?>
<?cocoon-process type="xslt"?>
| |
| |
目標(biāo)
數(shù)據(jù)
處理目標(biāo)名稱(chēng)
(Target)
String target = pi.getTarget();
獲得所有數(shù)據(jù)(
data
),在目標(biāo)(
target
)以后的所有數(shù)據(jù)都會(huì)被返回。
String data = pi.getData();
獲得指定屬性的數(shù)據(jù)
String type = pi.getValue("type");
獲得所有屬性的名稱(chēng)
List ls = pi.getNames();
6.
命名空間操作
<xhtml:html
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xhtml:title>Home Page</xhtml:title>
</xhtml:html>
Namespace xhtml = Namespace.getNamespace("xhtml", "http://www.w3.org/1999/xhtml");
List kids = html.getChildren("title", xhtml);
Element kid = html.getChild("title", xhtml);
kid.addContent(new Element("table", xhtml));
7.XSLT
格式轉(zhuǎn)換
使用以下函數(shù)可對(duì)
XSLT
轉(zhuǎn)換
最后如果你需要使用
w3c
的
Document
則需要轉(zhuǎn)換一下。
public static Document transform(String stylesheet
,
Document in)
throws JDOMException {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getDeocument();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}
參考書(shū)目:
1.JDOM
官方網(wǎng)站:
http://www.jdom.org
2.<<Processing XML with Java>> Elliotte Rusty Harold 2002
3.JDOM API Documentation
4.<<JDOM Makes XML Easy>>Jason Hunter Co-Creator JDOM Project
5.WSDP Tutorial