dom4j是一個優秀的開源專案,專用於xml檔案的讀寫,而且支援強大的xpath.
下面的程式碼演示了對xml檔案demo.xml的讀寫
demo.xml內容:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <address-list> 3 <card name="yangjm" id="1"> 4 <sex>男</sex> 5 <address><![CDATA[中國上海外灘No.01]]> </address> 6 <telephone>13916732212</telephone> 7 </card> 8 <card name="zhangsan" id="2"> 9 <sex>女</sex> 10 <address> 11 <item type="家庭地址"><![CDATA[中國.北京.東直門外大街]]> </item> 12 <item type="單位地址"><![CDATA[中國.上海.田林路888號]]> </item> 13 </address> 14 <telephone>010-123123</telephone> 15 </card> 16 </address-list>
java程式碼:
1 package jmyang.dom4j; 2 3 /** 4 * <p>Title:dom4j測試 </p> 5 * <p>Description: 使用xpath讀取節點內容、屬性</p> 6 * <p>Copyright: Copyright (c) 2012</p> 7 * <p>Company: infosky.com.cn</p> 8 * @author:yjmyzz@126.com 菩提樹下的楊過 9 * @version 1.0 10 */ 11 12 import java.util.*; 13 import java.io.*; 14 import org.dom4j.*; 15 import org.dom4j.io.*; 16 17 18 public class XmlTest { 19 20 21 public static void Test() { 22 String fileName = System.getProperty("user.dir") + "\\demo.xml"; //當前路徑下的demo.xml 23 24 try { 25 SAXReader reader = new SAXReader(); 26 Document doc = reader.read(fileName); //載入xml檔案 27 28 List peoples = doc.selectNodes("//*[@name]"); //選擇所有具有name屬性的節點(即demo.xml中的所有card節點) 29 for (Iterator iter = peoples.iterator(); iter.hasNext(); ) { 30 Element card = (Element) iter.next(); 31 //System.out.println(node.asXML()); 32 //System.out.println("---------------------------------------"); 33 //System.out.println(node.attributeCount()); 34 List attrList = card.attributes(); 35 //輸出每個card的所有屬性 36 for (Iterator attr = attrList.iterator(); attr.hasNext(); ) { 37 Attribute a = (Attribute) attr.next(); 38 System.out.println(a.getName() + "=" + a.getValue()); 39 40 } 41 System.out.println( 42 "----------------------------------------------------"); 43 } 44 45 Element zhangsan = (Element) doc.selectSingleNode("//card[@id='2']"); //查詢“id屬性”=2的card元素 46 System.out.println("張三的名稱:" + zhangsan.attribute("name").getValue()); //輸出zhangsan的name屬性 47 48 Node addrFamily = zhangsan.selectSingleNode("./address/item[2]"); //選擇zhangsan元素下的address節點下的第2個item子節點 49 System.out.println("張三的單位地址:" + addrFamily.getStringValue()); //輸出cdata內容 50 51 System.out.println( 52 "----------------------------------------------------"); 53 //為zhangsan下增加二個節點 54 zhangsan.addElement("email").addAttribute("type", 55 "工作").addText("work@some-domain.com"); 56 zhangsan.addElement("email").addAttribute("type", 57 "私人").addCDATA("private@some-domain.com"); //設定CDATA內容 58 59 System.out.println(zhangsan.asXML()); //列印zhangsan節點的xml內容(除錯用) 60 System.out.println( 61 "----------------------------------------------------"); 62 63 //將上述改動儲存到檔案demo2.xml 64 FileWriter fileWriter = new FileWriter(System.getProperty( 65 "user.dir") + "\\demo2.xml"); 66 67 OutputFormat format = OutputFormat.createPrettyPrint(); //設定美觀的縮排格式,便於閱讀 68 //format = OutputFormat.createCompactFormat();//設定緊湊格式(消除多餘空格),便於下載 69 XMLWriter writer = new XMLWriter(System.out, format); 70 writer.setWriter(fileWriter); 71 writer.write(doc); 72 writer.close(); 73 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 78 } 79 }
執行結果:
name=yangjm
id=1
----------------------------------------------------
name=zhangsan
id=2
----------------------------------------------------
張三的名稱:zhangsan
張三的單位地址:中國.上海.田林路888號
----------------------------------------------------
<card name="zhangsan" id="2">
<sex>女</sex>
<address>
<item type="家庭地址"><![CDATA[中國.北京.東直門外大街]]> </item>
<item type="單位地址"><![CDATA[中國.上海.田林路888號]]> </item>
</address>
<telephone>010-123123</telephone>
<email type="工作">work@some-domain.com</email><email type="私人"><![CDATA[private@some-domain.com]]></email></card>
----------------------------------------------------
儲存後的demo2.xml
<?xml version="1.0" encoding="UTF-8"?>
<address-list>
<card name="yangjm" id="1">
<sex>男</sex>
<address><![CDATA[中國上海外灘No.01]]> </address>
<telephone>13916732212</telephone>
</card>
<card name="zhangsan" id="2">
<sex>女</sex>
<address>
<item type="家庭地址"><![CDATA[中國.北京.東直門外大街]]> </item>
<item type="單位地址"><![CDATA[中國.上海.田林路888號]]> </item>
</address>
<telephone>010-123123</telephone>
<email type="工作">work@some-domain.com</email>
<email type="私人"><![CDATA[private@some-domain.com]]></email>
</card>
</address-list>
最後注意一點:dom4j支援xpath語法,但該特性需要jaxen包的支援(編譯期不需要,僅執行時需要),所以需要在專案中引入該包,如果是maven專案,要加入
<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.3-redhat-2</version> </dependency>
否則執行時會提示"類找不到"之類的錯誤