第一個程序,用Java代碼生成xml文檔,代碼以下:java
package com.example.xml.dom4j;import java.io.FileOutputStream;import java.io.FileWriter;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;/** * dom4j框架學習 使用dom4j框架建立xml文檔並輸出保存 * */public class Dom4JTest1 { public static void main(String[] args) throws Exception { // 第一種方式:建立文檔,並建立根元素 // 建立文檔:使用了一個Helper類 Document document = DocumentHelper.createDocument(); // 建立根節點並添加進文檔 Element root = DocumentHelper.createElement("student"); document.setRootElement(root); // 第二種方式:建立文檔並設置文檔的根元素節點 Element root2 = DocumentHelper.createElement("student"); Document document2 = DocumentHelper.createDocument(root2); // 添加屬性 root2.addAttribute("name", "zhangsan"); // 添加子節點:add以後就返回這個元素 Element helloElement = root2.addElement("hello"); Element worldElement = root2.addElement("world"); helloElement.setText("hello Text"); worldElement.setText("world text"); // 輸出 // 輸出到控制檯 XMLWriter xmlWriter = new XMLWriter(); xmlWriter.write(document); // 輸出到文件 // 格式 OutputFormat format = new OutputFormat(" ", true);// 設置縮進爲4個空格,而且另起一行爲true XMLWriter xmlWriter2 = new XMLWriter( new FileOutputStream("student.xml"), format); xmlWriter2.write(document2); // 另外一種輸出方式,記得要調用flush()方法,不然輸出的文件中顯示空白 XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student2.xml"), format); xmlWriter3.write(document2); xmlWriter3.flush(); // close()方法也能夠 } }
程序Console輸出:框架
<?xml version="1.0" encoding="UTF-8"?> <student/>
生成的一個xml文檔:dom
<?xml version="1.0" encoding="UTF-8"?><student name="zhangsan"> <hello>hello Text</hello> <world>world text</world></student>
程序實例2,讀入xml文檔並分析,將其內容輸出。學習
首先,待分析的文檔以下:ui
<?xml version="1.0" encoding="UTF-8"?><students name="zhangsan"> <hello name="lisi">hello Text1</hello> <hello name="lisi2">hello Text2</hello> <hello name="lisi3">hello Text3</hello> <world name="wangwu">world text1</world> <world name="wangwu2">world text2</world> <world >world text3</world></students>
Java代碼:spa
package com.example.xml.dom4j;import java.io.File;import java.util.Iterator;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.DOMReader;import org.dom4j.io.SAXReader;/** * dom4j框架學習: 讀取並解析xml * * */public class Dom4JTest2 { public static void main(String[] args) throws Exception { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("students.xml")); // 獲取根元素 Element root = document.getRootElement(); System.out.println("Root: " + root.getName()); // 獲取全部子元素 List<Element> childList = root.elements(); System.out.println("total child count: " + childList.size()); // 獲取特定名稱的子元素 List<Element> childList2 = root.elements("hello"); System.out.println("hello child: " + childList2.size()); // 獲取名字爲指定名稱的第一個子元素 Element firstWorldElement = root.element("world"); // 輸出其屬性 System.out.println("first World Attr: " + firstWorldElement.attribute(0).getName() + "=" + firstWorldElement.attributeValue("name")); System.out.println("迭代輸出-----------------------"); // 迭代輸出 for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element e = (Element) iter.next(); System.out.println(e.attributeValue("name")); } System.out.println("用DOMReader-----------------------"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // 注意要用完整類名 org.w3c.dom.Document document2 = db.parse(new File("students.xml ")); DOMReader domReader = new DOMReader(); // 將JAXP的Document轉換爲dom4j的Document Document document3 = domReader.read(document2); Element rootElement = document3.getRootElement(); System.out.println("Root: " + rootElement.getName()); } }
代碼運行後輸出:.net
Root: students total child count: 6hello child: 3first World Attr: name=wangwu 迭代輸出----------------------- lisi lisi2 lisi3 wangwu wangwu2 null 用DOMReader----------------------- Root: students
jar包下載 http://download.csdn.net/download/zsy_fengzhiying/4175530code