SAXReader reader = new SAXReader();
Document document = reader.read(new File("input.xml"));
2.解析XML形式的文字,得到document物件.
String text = "<members></members>";
Document document = DocumentHelper.parseText(text);
3.主動建立document物件.
Document document = DocumentHelper.createDocument();
//建立根節點
Element root = document.addElement("members");
節點物件:
Element root = document.getRootElement();
Element element=node.element(“書名");
String text=node.getText();
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
Element element = (Element) it.next();
// do something
}
6.在某節點下新增子節點.
Element ageElm = newMemberElm.addElement("age");
element.setText("29");
//childElm是待刪除的節點,parentElm是其父節點
parentElm.remove(childElm);
Element contentElm = infoElm.addElement("content");
contentElm.addCDATA(diary.getContent());
Element root=document.getRootElement();
//屬性名name
Attribute attribute=root.attribute("size");
2.取得屬性的文字
Attribute attribute=root.attribute("size");
root.remove(attribute);
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
5.設定某節點的屬性和文字.
6.設定屬性的文字
attribute.setText("sitinspring");
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(document);
writer.close();
2.文件中含有中文,設定編碼格式寫入的形式.
// 指定XML編碼
format.setEncoding("GBK");
XMLWriter
writer = new XMLWriter(newFileWriter("output.xml"),format);
writer.write(document);
writer.close();
字串與XML的轉換 :
String text = "<members> <member>sitinspring</member></members>";
Document document = DocumentHelper.parseText(text);
2.將文件或節點的XML轉化為字串.
SAXReader reader = new SAXReader();
Document document = reader.read(new
File("input.xml"));
Element root=document.getRootElement();
String docXmlText=document.asXML();
String rootXmlText=root.asXML();
Element memberElm=root.element("member");
String memberXmlText=memberElm.asXML();
首先需要匯入這兩個jar包:
第一個是必須的,第二個是為後邊使用XPath使用的。
對XML文件的CRUD操作:
package com.dom4j.cn;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class Dom4JDemo {
/*
* 讀取指定標籤的內容,以及他的屬性
*/
@Test
public void read1() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element book = (Element) root.elements("書").get(0);
String value = book.element("書名").getText();
System.out.println("標籤內容:" + value);
Attribute attr = book.element("書名").attribute("name");
System.out.println("標籤屬性 : " + attr.getName() + " 屬性內容 : " + attr.getText());
}
/*
* 在標籤末尾新增標籤
*/
@Test
public void add1() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element book = root.element("書");
book.addElement("售價").setText("100005415");
//利用Format解決亂碼問題。 注意XMLWriter的第一個引數要使用位元組流
//如果它使用字元流的話,那麼一定要指定該字元流的碼錶與format的一致
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
writer.write(document);
writer.close();
/*
* 字元流的形式
*/
/*OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8"), format);
writer.write(document);
writer.close();*/
}
/*
* 在指定標籤的後邊新增標籤
*/
@Test
public void add2() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element book = root.element("書");
List list = book.elements();
Element price = DocumentHelper.createElement("加價");
price.setText("500"); //設定新標籤的內容
price.addAttribute("ok", "yes"); // 設定新標籤的屬性
list.add(2, price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
writer.write(document);
}
/*
* 刪除指定標籤 或者 屬性
*/
@Test
public void delete() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
//刪除標籤
/*Element price = root.element("書").element("售價");
price.getParent().remove(price);*/
Element bookName = root.element("書").element("書名");
bookName.remove(bookName.attribute("name"));
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
writer.write(document);
}
/*
* 更新指定標籤內容或者屬性
*/
@Test
public void update() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
//更新標籤內同
// root.element("書").element("書名").setText("歐亞");
//更新標籤屬性
root.element("書").element("書名").attribute("name").setText("qqqq");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
writer.write(document);
}
}
XMLPath類似於正則有非常強大的功能來取標籤, 這裡簡單模擬一個登陸判斷使用者名稱密碼的。 可以檢視API
package com.dom4j.cn;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XMLPathDemo {
public static void main(String[] args) throws DocumentException {
String username = "aa";
String password = "111";
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml"));
Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
if (node == null) {
System.out.println("使用者名稱密碼錯誤");
} else {
System.out.println("登陸成功");
}
}
}
XPathAPI :http://pan.baidu.com/s/1i3zDpNv
DOM4J jar包: http://pan.baidu.com/s/1sj6O5R3 http://pan.baidu.com/s/1c0Fw81a