Java XML解析工具 JDOM介绍及使用实例
?
Java XML解析工具 JDOM
?
JDOM介紹
JDOM是一種使用XML的獨特Java工具包,用于快速開發XML應用程序。
它的設計包含Java語言的語法乃至語義。
JDOM是一個開源項目,它基于樹形結構,利用純Java的技術對XML文檔實現解析、生成、序列化及多種操作。
?
JAXP
JAXP(用于XML語法分析的Java API)包含了三個包:
org.w3c.dom
W3C推薦的用于XML標準文檔對象模型的Java工具。
org.xml.sax
用于對XML進行語法分析的事件驅動的簡單API。
javax.xml.parsers
工廠化工具,允許應用程序開發人員獲得并配置特殊的語法分析器工具,JDOM能夠替換org.w3c.dom軟件包來有計劃地操作XML文檔。
?
JDOM和DOM及SAX的關系
JDOM主要用來彌補DOM和SAX在實際應用當中的不足。
主要是SAX沒有文檔修改、隨機訪問及輸出的功能;
而DOM,Java程序員在使用時總覺得不太方便(比如DOM定義了自己的Text類而不是使用Java的String)。
?
DOM的局限性:
DOM的缺點主要是由于DOM是一個接口定義語言(IDL),它的任務是在不同語言實現中的一個最低的通用標準,并不是為Java特別設計的。
DOM API沿襲了XML規范,在XML中,每件東西都是一個結點,因此在DOM中找到的幾乎每件東西都基于Node接口。
就多態性來講,它是優秀的,但鑒于如上解釋,它在Java語言中的應用是困難而且不便的,其中從Node向葉類型作顯式向下類型轉換會導致代碼的冗長和難以理解。
?
JDOM是作為一種輕量級的API被制定的,最主要的是它是以Java為中心的。它在遵循DOM主要規則的基礎上除去了上述缺點。
JDOM是Java平臺專用的,只要有可能,API都使用Java語言的內建String支持。
在JDOM中,XML元素就是Element的實例,XML屬性就是Attribute的實例,XML文檔本身就是Document的實例。
類驅動:因為JDOM對象就是像Document、Element和Attribute這些類的直接實例,因此創建一個新的JDOM對象就如在Java語言中使用new操作符一樣容易。
它還意味著不需要進行工廠化接口配置——JDOM的使用是直截了當的。
?
JDOM使用程序實例
實例1,使用JDOM,用Java代碼構造一個XML文檔:
注:使用前需要下載JDOM的jar包(我用的是jdom-2.0.5.jar)并且把它加載在類路徑里面(Properties->Java Build Path -> Add External JARs...)
Java程序如下,詳細說明見代碼注釋:
?
package com.example.xml.jdom;import java.io.FileOutputStream;import org.jdom2.Attribute; import org.jdom2.Comment; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter;/*** 使用JDOM構造一個XML文檔,并輸出* */ public class JDOMTest1 {public static void main(String[] args) throws Exception{// 文檔Document document = new Document();// 生成一個元素Element root = new Element("root");// 將生成的元素加入文檔:根元素 document.addContent(root);// 給結點加上注釋Comment comment = new Comment("This is my comments");root.addContent(comment);// 加上子元素1Element e = new Element("hello");// 加屬性e.setAttribute("sohu", "www.sohu.com");root.addContent(e);// 加子元素2Element e2 = new Element("world");Attribute attribute = new Attribute("testAttr", "attr Value");e2.setAttribute(attribute);// set方法會返回元素本身(方法鏈method chain style) root.addContent(e2);e2.addContent(new Element("subElement").setAttribute("a", "aValue").setAttribute("x", "xValue").setAttribute("y", "yValue").setText("textContent"));// 格式化Format format = Format.getPrettyFormat();// Format.getRawFormat()方法,通常用于XML數據的網絡傳輸,因為這種格式會去掉所有不必要的空白,因此能夠減少數據量// 可以自己設定一些format的屬性format.setIndent(" ");// 把縮進設為四個空格(默認為兩個空格)// 輸出XMLOutputter out = new XMLOutputter(format);out.output(document, new FileOutputStream("jdom.xml"));// 可在當前項目路徑下找到 } }?
?
刷新后可以看到項目路徑下生成了文檔jdom.xml:
<?xml version="1.0" encoding="UTF-8"?> <root><!--This is my comments--><hello sohu="www.sohu.com" /><world testAttr="attr Value"><subElement a="aValue" x="xValue" y="yValue">textContent</subElement></world> </root>?
?
?
實例2,使用JDOM解析并操縱XML文檔:
?
就讀入上面創建的文檔,Java代碼:
?
package com.example.xml.jdom;import java.io.File; import java.io.FileWriter; import java.util.List;import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter;/*** 讀取一個XML文檔* */ public class JDOMTest2 {public static void main(String[] args) throws Exception{// 構造器SAXBuilder saxBuilder = new SAXBuilder();// 獲取文檔Document document = saxBuilder.build(new File("jdom.xml"));// 得到根元素Element element = document.getRootElement();System.out.println("Root: " + element.getName());// 獲取子元素Element hello = element.getChild("hello");System.out.println("child: " + hello.getName());// 獲取屬性List<Attribute> list = hello.getAttributes();for (int i = 0; i < list.size(); ++i){Attribute attr = (Attribute) list.get(i);String attrName = attr.getName();String attrValue = attr.getValue();System.out.println("hello的屬性: " + attrName + " = " + attrValue);}// 移除一個元素element.removeChild("world");// 設定格式XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent(" "));out.output(document, new FileWriter("jdom2.xml"));// FileWriter和FileOutputStream都能用 }}?
另存后的jdom2.xml:
<?xml version="1.0" encoding="UTF-8"?> <root><!--This is my comments--><hello sohu="www.sohu.com" /> </root>?
?
?
參考資料
圣思園張龍老師XML教學視頻。
JDOM官方網站:
http://www.jdom.org/
JDOM 2.0.5(目前2013.06.02最新版)文檔:
http://www.jdom.org/docs/apidocs/
?
總結
以上是生活随笔為你收集整理的Java XML解析工具 JDOM介绍及使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node.js初探-超越昨天的自己系列(
- 下一篇: 【Android】CM在repo中使用l