book.xml
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名>Java就业基础教程</书名>
<作者>张孝祥</作者>
<售价>48元</售价>
<售价>58元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>黎活明</作者>
<售价>46元</售价>
</书>
</书架>
Dom4j.java
package yyy.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.dom4j.Document;
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 Dom4j {
private File file = new File("src/book.xml");
//遍历xml文档
@Test
public void testList() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
list(root);
}
public void list(Element element){
System.out.println(element.getName());
List<Node> nodes = element.elements();
//System.out.println(nodes.size());
for(Node node : nodes){
if(node instanceof Element){
list((Element) node);
}
}
}
//读取某个节点的值
@Test
public void read() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
Element price = root.element("书").element("作者");
String value = price.getText();
System.out.println(value);
}
//添加某个节点
@Test
public void add() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element price = DocumentHelper.createElement("售价"); //创建新的节点
price.setText("44元"); //设置节点值
Element root = document.getRootElement();
root.element("书").add(price); //添加节点
//将内存中的document对象重新写入xml文档 特别要注意乱码问题
//方法1:使用OutputStreamWriter设置写入文档时所使用的编码表
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
document.write(out);
out.close();
/*//方法2:
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter(file),format);
writer.write(document);
writer.close();*/
/*//方法3:
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format);
writer.write(document);
writer.close();*/
/*//方法4:
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312"); //源文档用"gb2312"编码,既不改变源文档的编码格式,又不能有乱码
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
writer.write(document);
writer.close();*/
/*//document对象在内存中是以"UTF-8"编码形式存在,用FileWriter将document对象以字符流的形式写入xml文档默认是用本地码表"gb2312"编码
* 乱码问题总结:
* 1.用字符流向文件写入数据要考虑乱码问题,而用字节流就不必考虑乱码问题
* 用字符流向文件写入数据默认使用本地码表即"gb2312"
* 2.任何对象读入内存都是以"UTF-8"编码的形式存在
* 默认情况下XMLWriter的write方法是以"UTF-8"的编码形式将内存中的document对象传给FileWriter,所以要想不发生乱码问题,
* 就要使用包装流OutputStreamWriter并给定写入文件时所使用的编码表,或者使用OutputFormat的setEncoding方法指定传给
* 流对象时所使用的编码格式。
* */
}
//向指定位置添加节点
@Test
public void add2() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
Element price = DocumentHelper.createElement("售价");
price.setText("48元");
List list = root.element("书").elements();
list.add(2,price);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
writer.write(document);
writer.close();
}
//删除某个节点
@Test
public void delete() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
Element price = root.element("书").element("售价");
price.getParent().remove(price);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
writer.write(document);
writer.close();
}
//更改某个节点的内容
@Test
public void update() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
Element price = root.element("书").element("售价");
price.setText("58元");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
writer.write(document);
writer.close();
}
}