XML解析之DOM4J

package cn.itcast.dom4j;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class DOM4JTest {
    public static void main(String[] args) throws DocumentException, IOException {
        xPathDemo3();
    }
      
    /**
     * 使用XPath查找节点 找第二本书的书名
     * @throws DocumentException
     */
    public static void xPathDemo3() throws DocumentException {
        // 解析器
        SAXReader saxReader =  new SAXReader();
        // 解析文档
        Document document = saxReader.read("src/book.xml");
        // 获取所有 书名 节点
        @SuppressWarnings("unchecked")
        List<Element> elementsList = document.selectNodes("//书名");
        System.out.println(elementsList.get(3).getText());
          
        // 直接找到某本书的书名
        Node node = document.selectSingleNode("//书名[@id='b1']");
        System.out.println(node.getText());
    }
      
    /**
     * 查询第一本书
     * @throws DocumentException
     */
    public static void demo1() throws DocumentException {
        // 创建解析器
        SAXReader saxReader = new SAXReader();
        // 解析文档
        Document document = saxReader.read("src/book.xml");
        // 先获得根节点
        Element rooteElement = document.getRootElement();
        // 查找根节点下的节点
        String text = rooteElement.element("书").element("书名").getText();
        // 打印输出
        System.out.println(text);
    }
    /**
     * 添加元素
     * @throws DocumentException
     * @throws IOException
     */
    public static void add() throws DocumentException, IOException {
        // 创建解析器
        SAXReader saxReader = new SAXReader();
        // 解析文档
        Document document = saxReader.read("src/book.xml");
        // 获得根节点
        Element rootElement = document.getRootElement();
        // 找到要添加的节点
        rootElement.element("书").addElement("书名").setText("武当剑法");
        // 回写 完美格式
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        /*// 回写 压缩格式
        OutputFormat outputFormat = OutputFormat.createCompactFormat();*/
        outputFormat.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"), outputFormat);
        writer.write(document);
        writer.close();
    }
      
    /**
     * 修改元素:把 葵花宝典 改成 武当剑法
     * @throws DocumentException
     * @throws IOException
     */
    public static void modified() throws DocumentException, IOException {
        // 创建解析器
        SAXReader saxReader = new SAXReader();
        // 解析文档
        Document document = saxReader.read("src/book.xml");
        // 获得根节点
        Element rooElement = document.getRootElement();
        // 修改
        rooElement.element("书").element("书名").setText("武当剑法");
          
        // 回写 完美格式
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/book.xml"), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
      
    /**
     * 删除第一本书的书名
     * @throws DocumentException
     * @throws IOException
     */
    public static void delete() throws DocumentException, IOException {
        // 解析器
        SAXReader reader = new SAXReader();
        // 解析文档
        Document document = reader.read("src/book.xml");
        // 获得根节点
        Element root = document.getRootElement();
        // 获得第一本书这个节点
        Element element = root.element("书").element("书名");
        // 删除书名
        element.getParent().remove(element);
          
        // 回写 完美格式
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/book.xml"), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}


本文出自 “狐灵传说” 博客,转载请与作者联系!

你可能感兴趣的:(import,public)