Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath
转:
Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj626852095/article/details/17002321
Dom4j 说明文档:http://blog.csdn.net/xj626852095/article/details/17000741
xPath说明文档: http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html
要解析的xml文件
-
-
-
<书架>
-
<书 name="yyyyyyy">
-
<书名>Java</书名>
-
<作者>张三</作者>
-
<打折价>9.9折</打折价>
-
<售价>只要998</售价>
-
</书>
-
<书 memony="有优惠价">
-
<书名 name="xxxxx">JavaScript网页开发</书名>
-
<作者>李四</作者>
-
<售价>28.00元</售价>
-
<优惠价>8折</优惠价>
-
<优惠价>8折</优惠价>
-
<优惠价>8折</优惠价>
-
</书>
-
<user username="kevin" passwd="123456"/>
-
<user username="xiang" passwd="234567"/>
-
</书架>
-
package com.kevin.java;
-
-
import static org.junit.Assert.*;
-
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
import java.io.OutputStreamWriter;
-
import java.util.List;
-
-
import org.dom4j.Document;
-
import org.dom4j.DocumentException;
-
import org.dom4j.DocumentHelper;
-
import org.dom4j.Element;
-
import org.dom4j.Node;
-
import org.dom4j.io.OutputFormat;
-
import org.dom4j.io.SAXReader;
-
import org.dom4j.io.XMLWriter;
-
import org.junit.Test;
-
import org.xml.sax.DocumentHandler;
-
-
public class Dom4j_Test {
-
-
//Xpath使用
-
-
public void test4() throws DocumentException, IOException {
-
SAXReader reader = new SAXReader();
-
Document document = reader.read(new File("src/book1.xml"));
-
String bookName = document.selectSingleNode("//书名").getText();
-
System.out.println("bookName: " + bookName);
-
-
//查找符合条件的User //BBB[@id='b1'] 注意要有单引号
-
String username = "kevin";
-
String passwd = "123456";
-
Node node = document.selectSingleNode
-
("//user[@username='"+username+"' and @passwd='"+passwd+"']");
-
if(node==null)
-
System.out.println("不是注册用户");
-
else
-
System.out.println("注册用户");
-
-
}
-
-
//获得第二本数的书名, 属性值 , 添加售价节点
-
-
public void test1() throws DocumentException, IOException {
-
SAXReader reader = new SAXReader();
-
Document document = reader.read(new File("src/book1.xml"));
-
Element root = document.getRootElement();
-
Element book = root.elements("书").get(1);
-
String bookName = book.element("书名").getText();
-
String bookAttr = book.element("书名").attributeValue("name");
-
System.out.println("bookName: " + bookName);
-
System.out.println("bookAttr: " + bookAttr);
-
-
//添加售价节点
-
book.addAttribute("memony", "有优惠价");
-
book.addElement("优惠价").setText("8折");
-
OutputFormat format = OutputFormat.createPrettyPrint();
-
format.setEncoding("UTF-8");
-
XMLWriter writer = new XMLWriter(
-
new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
-
writer.write(document);
-
writer.close();
-
}
-
-
//指定位置添加
-
-
public void test2() throws DocumentException, IOException {
-
SAXReader reader = new SAXReader();
-
Document document = reader.read(new File("src/book1.xml"));
-
Element book = document.getRootElement().element("书");
-
List list = book.elements();
-
-
Element price = DocumentHelper.createElement("打折价");
-
price.setText("9.9折");
-
list.add(2, price);
-
-
OutputFormat format = OutputFormat.createPrettyPrint();
-
format.setEncoding("UTF-8");
-
XMLWriter writer = new XMLWriter(
-
new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
-
writer.write(document);
-
writer.close();
-
}
-
-
//删除节点 更新节点
-
-
public void test3() throws DocumentException, IOException {
-
SAXReader reader = new SAXReader();
-
Document document = reader.read(new File("src/book1.xml"));
-
Element priceTmp = document.getRootElement().element("书").element("打折价");
-
//删除节点
-
priceTmp.getParent().remove(priceTmp);
-
-
Element price = document.getRootElement().element("书").element("售价");
-
//更新节点
-
price.setText("只要998");
-
-
OutputFormat format = OutputFormat.createPrettyPrint();
-
format.setEncoding("UTF-8");
-
XMLWriter writer = new XMLWriter(
-
new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
-
writer.write(document);
-
writer.close();
-
}
-