dom4j解析xml文档(增删查改)

来源:互联网 发布:怎么编程shell 编辑:程序博客网 时间:2024/05/19 05:06
package cn.itcast.dom4j;


import java.awt.print.Book;
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;


public class Demo1 {


//读取xml文档第二本书的,<书名>JavaScript网页开发</书名>

public void read() throws Exception{
SAXReader reader = new SAXReader();
        Document document = reader.read("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="xxx">JavaScript网页开发</书名>


public void read1() throws Exception{
SAXReader reader = new SAXReader();
        Document document = reader.read("src/book.xml");
        
        Element root = document.getRootElement();
        Element book = (Element) root.elements("书").get(1);
        String value = book.element("书名").attributeValue("name");
        System.out.println(value);
}
//在第一本书上添加一个新的售价<售价>200元</售价>

public void add() throws Exception{
SAXReader reader = new SAXReader();
        Document document = reader.read("src/book.xml");
        
        Element book = document.getRootElement().element("书");
        book.addElement("售价").setText("200元");
      
//        OutputFormat format = OutputFormat.createCompactFormat();//紧凑的
//        format.setEncoding("gb2312");
//        XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format);
        
        
        //这样编码才不会乱码
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8"));
      //  XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"));
       // 不管xml文档是什么编码保存的,都会乱码
        writer.write( document );
        writer.close();
}
//在第一本书上的指定位置添加一个新的售价<售价>200元</售价>

public void add2() throws Exception{
SAXReader reader = new SAXReader();
       Document document = reader.read("src/book.xml");
       
       Element book = document.getRootElement().element("书");
       List list = book.elements();
       
       Element price = DocumentHelper.createElement("售价");
       price.setText("300元");
       
       OutputFormat format = OutputFormat.createPrettyPrint();
       format.setEncoding("UTF-8");
       
       XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"));
       writer.write(document);
       writer.close();
       
}



       public void delete() throws Exception{
SAXReader reader = new SAXReader();
       Document document = reader.read("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"));
       writer.write(document);
       writer.close();
       }
       
       //改
       @Test
public void update() throws Exception{
SAXReader reader = new SAXReader();
       Document document = reader.read("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"));
       writer.write(document);
       writer.close();
}
}
0 0