标签:
package com.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
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.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import org.xml.sax.DocumentHandler;
public class Demo1 {
//读取xml文档第二本书的:<书名>javaScript网页开发</书名>
@Test
public void read() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element root = document.getRootElement();
Element book = (Element) root.elements("书").get(1);
String value = book.element("书名").getText();
System.out.println(value);
}
//读取xml文档中第一本书的name属性值
@Test
public void read2() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element root = document.getRootElement();
Element book = (Element) root.elements("书").get(0);
String value = book.element("书名").attribute("name").getValue();
System.out.println(value);
}
//在第一本书上添加一个新的售价
@Test
public void add() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = document.getRootElement().element("书");
book.addElement("售价").setText("209元");
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer =new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
//在第一本书上指定位置添加一个新的售价
@Test
public void add2() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = document.getRootElement().element("书");
List list = book.elements();
Element price = DocumentHelper.createElement("售价");
price.setText("309元");
list.add(2,price);
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer =new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
//在第2本书上指定位置添加一个新的售价
@Test
public void add3() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = (Element) document.getRootElement().elements("书").get(1);
List list = book.elements();
Element price = DocumentHelper.createElement("售价");
price.setText("309元");
list.add(2,price);
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer =new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
//删除上面的节点
@Test
public void delete() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element price = document.getRootElement().element("书").element("售价");
price.getParent().remove(price);
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer =new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
//更新
@Test
public void update() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = (Element) document.getRootElement().elements("书").get(1);
book.element("作者").setText("撒旦");
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer =new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名 name="xxx">Java就业培训教程</书名>
<作者>张孝祥</作者>
<售价>59.00元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>撒旦</作者>
<售价>28.00元</售价>
</书>
</书架>
标签:
原文地址:http://www.cnblogs.com/Dnice/p/5494942.html