1,首先要导入dom4j.jar包.
2,要注意编码的问题,
book.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <书架>
- <书>
- <书名 name="xx">Java就业培训教程</书名>
- <作者>张孝祥</作者>
- <售价>38.00元</售价>
- <售价>28.00元</售价>
- <售价>209元</售价>
- </书>
- <书>
- <书名>JavaScript网页开发</书名>
- <作者>李货名</作者>
- <售价>109.00元</售价>
- <售价>209元</售价>
- <售价>209元</售价>
- <售价>209元</售价>
- <售价>209元</售价>
- <售价>209元</售价>
- <售价>209元</售价>
- </书>
- <书>
- <书名>JavaWeb开发</书名>
- <作者>张孝祥</作者>
- <售价>28.00元</售价>
- </书>
- </书架>
Demo1.java:
- package cn.hmm.day4;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- 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;
- public class Demo1 {
- //读取xml文档中第二本书的书名
- @Test
- public void read() throws DocumentException{
- 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("书名").getText();
- //获得属性值
- String value = book.element("书名").attribute("name").getValue();
- System.out.println(value);
- }
- //在第一本书上添加一个新的售价
- @Test
- public void add() throws DocumentException, IOException{
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File("src/book.xml"));
- Element root = document.getRootElement();
- Element book = (Element)root.elements().get(1);
- book.addElement("售价").setText("209元");
- //把数据写入到xml中去
- //解决乱码问题 乱码:XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"));
- //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8"));
- //格式化输出
- /*OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("gb2312");
- XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format);*/
- //第二种格式化输出
- OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
- writer.write(document);
- writer.close();
- }
- //在指定位置添加一个新的售价
- @Test
- public void add1() throws DocumentException, IOException{
- 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.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
- writer.write(document);
- writer.close();
- }
- //删除上门添加的节点
- @Test
- public void delete() throws DocumentException, IOException{
- 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.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
- writer.write(document);
- writer.close();
- }
- //更新第二本书的作者名字
- @Test
- public void update() throws DocumentException, IOException{
- 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.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
- writer.write(document);
- writer.close();
- }
- }