java(12)--xml之dom4j方式的增删改查

package zgq.dom4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.dom4j.Document;
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 xmlofdom4j {

    // 运用dom4j进行dom的查找
    // 读取xml文档第二本书的<书名 name="xxxxx">Java网络编程精解</书名>
    @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="xxxxx">Java网络编程精解</书名>
    @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(1);

        String value = book.element("书名").attributeValue("name");
        System.out.println(value);

    }

    // 在第一本上添加一个新的售价:<售价>209元</售价> dom4j sun io
    @Test
    public void add() 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);
        book.addElement("售价1").setText("208元");

        // 需要将元素写进去
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
                format);
        writer.write(document);
        writer.close();
    }

    // 在第一本指定位置上添加一个新的售价:<售价>209元</售价> 更改保存了所有孩子的list集合的顺序
    @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.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 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.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 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("zhangsan");

        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        XMLWriter writer=new XMLWriter(new FileOutputStream("src/book.xml"),format);
        writer.write(document);
        writer.close(); 
    }
}

你可能感兴趣的:(java,dom)