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

java(12)--xml之dom方式的增删改查_第1张图片

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class domofcurd {

    // 使用dom方式对xml文件进行crud 增删改查

    @Test
    // 读取xml文档中:<书名>Java网络编程精解</书名> 节点中的值
    public void read1() throws Exception {
        // 创建解析工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // 创建构造器
        DocumentBuilder builder = factory.newDocumentBuilder();
        // 得到xml文件
        Document document = builder.parse("src/book.xml");

        NodeList list = document.getElementsByTagName("书名");
        Node item = list.item(1);// 选取第二个
        String content = item.getTextContent();
        System.out.println(content);
    }

    // 得到xml文档中的所有标签
    @Test
    public void read2() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        // 得到根节点
        Node rootNode = document.getElementsByTagName("书架").item(0);

        list(rootNode);

    }

    public void list(Node node) {
        if (node instanceof Element) {
            System.out.println(node.getNodeName());
        }
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node childNode = list.item(i);
            list(childNode);
        }
    }

    // 得到xml文档中标签属性的值:<书名 name="xxxx">Head First Java</书名>
    @Test
    public void read3() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder buider = factory.newDocumentBuilder();
        Document document = buider.parse("src/book.xml");

        Element bookname = (Element) document.getElementsByTagName("书名")
                .item(0);
        String value = bookname.getAttribute("name");
        System.out.println(value);
    }

    // 向xml文档中添加节点:<售价>59.00元</售价>
    @Test
    public void add() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder buider = factory.newDocumentBuilder();
        Document document = buider.parse("src/book.xml");

        // 创建节点
        Element price = document.createElement("售价");
        price.setTextContent("59.00元");

        // 将节点挂在第一本书上
        Element booknode = (Element) document.getElementsByTagName("书").item(0);
        booknode.appendChild(price);

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer transformer = tffactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }

    // 向xml文档中指定位置上添加节点:<售价>59.00元</售价>
    @Test
    public void add2() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        // 创建节点
        Element price = document.createElement("售价");
        price.setTextContent("59.00元");

        // 得到参考节点
        Element refNode = (Element) document.getElementsByTagName("售价").item(0);

        // 得到要插入节点的父节点---book节点
        Element book = (Element) document.getElementsByTagName("书").item(0);

        // 往book节点的指定位置插入子节点
        book.insertBefore(price, refNode);

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tf = tffactory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }

    // 向xml文档节点上添加属性:<书名>Java网络编程精解</书名> 上添加name=“xxx”属性
    @Test
    public void addAttr() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        Element bookname = (Element) document.getElementsByTagName("书名")
                .item(1);
        bookname.setAttribute("name", "xxxxx");

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tf = tffactory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }

    // 删除元素
    @Test
    public void delete1() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        // 得到要删除的结点
        Element e = (Element) document.getElementsByTagName("售价").item(0);

        // 得到要删除的结点的父节点
        Element book = (Element) document.getElementsByTagName("书").item(0);

        // 从父节点上移除子节点
        book.removeChild(e);

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tf = tffactory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }

    // 删除整个xml
    @Test
    public void delete2() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        // 得到要删除的结点
        Element element = (Element) document.getElementsByTagName("售价").item(0);

        element.getParentNode().getParentNode().getParentNode()
                .removeChild(element.getParentNode().getParentNode());

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tf = tffactory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }

    // 更新售价
    @Test
    public void update() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");

        Element e = (Element) document.getElementsByTagName("售价").item(0);
        e.setTextContent("109元");

        // 把更新后内存写回到xml文档
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tf = tffactory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(
                new FileOutputStream("src/book.xml")));
    }
}

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