package com.test.dom4j;
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;
public class Dom4jDemo {
@Test
public void readXml() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element bookname = root.element("书").element("书名");
System.out.println(bookname.getText());
}
@Test
public void readAttr() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
String bookattr = root.element("书").element("书名").attributeValue("name");
System.out.println(bookattr);
}
@Test
public void addElem() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element newelem = root.element("书").addElement("会员价");
newelem.setText("9");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"),format);
//此处如果乱码,可以换成FileOutputStream;
//XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void addElemTo() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
//创建一个新节点
Element newelem = DocumentHelper.createElement("高级会员价");
newelem.setText("99");
//将新节点挂到指定位置上
Element root = document.getRootElement();
List list = root.element("书").elements();
list.add(2, newelem);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"),format);
writer.write(document);
writer.close();
}
@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("19");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void delete() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element elem = root.element("书").element("会员价");
elem.getParent().remove(elem);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void findWithXpath() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
List list = document.selectNodes( "//书名" );
Element elem = (Element) list.get(0);
System.out.println(elem.getText());
//Node node = document.selectSingleNode( "//书/书名" );
//String name = node.valueOf( "@name" );
}
}
JAVA学习笔记(三)—— dom4j
最新推荐文章于 2022-03-29 19:46:41 发布