当前位置:网站首页>dom4j解析xml

dom4j解析xml

2022-06-10 11:57:00 wfsm

jar包

 <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.2.0</version>
        </dependency>

xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <author>Thomas</author>
        <title>Java从入门到放弃</title>
        <publisher>UCCU</publisher>
    </book>
    <book>
        <author>小白</author>
        <title>MySQL从删库到跑路</title>
        <publisher>Go Die</publisher>
    </book>
    <book>
        <author>PHPer</author>
        <title>Best PHP</title>
        <publisher>PHPchurch</publisher>
    </book>
</books>

方式一:root.elements()

  public static void main(String[] args) throws DocumentException {
    
        // 获取当前项目路径
        String url = System.getProperty("user.dir");
        System.out.println("url = " + url);
        SAXReader reader = new SAXReader();
        File file = new File(url + File.separator + "1.xml");
        Document fileDocument = reader.read(file);
        // 获取根节点
        Element root = fileDocument.getRootElement();
        // 获取根节点下所有子元素
        List<Element> childElements = root.elements();

        for (Element child : childElements) {
    
            List<Element> books = child.elements();
            for (Element book : books) {
    
                // 元素名
                String name = book.getName();
                // 元素值
                String text = book.getText();
                System.out.println(name+":"+text);
            }
        }

        Element book1 = childElements.get(0);
        Element author = book1.element("author");
        System.out.println("author is "+author.getText());
    }

方式二:document.selectNodes("路径下的节点")

   public static void main(String[] args) throws DocumentException, IOException {
    
        String url = System.getProperty("user.dir");
        SAXReader reader = new SAXReader();
        File file = new File(url + File.separator + "1.xml");
        Document document = reader.read(file);
        List<Node> authors = document.selectNodes("/books/book/author");
        if (!CollectionUtils.isEmpty(authors)){
    
            authors.iterator().forEachRemaining(item-> System.out.println(item.getText()));
        }

        Element root = document.getRootElement();
        // 必须在根节点下添加
        Element phone = root.addElement("phone");
        phone.addText("123");
        boolean book = root.remove(root.element("book"));
        System.out.println("book = " + book);

        // 将修改后的文档,写入xml中
        FileWriter fileWriter = new FileWriter(System.getProperty("user.dir") + File.separator + "1.xml");
        XMLWriter writer = new XMLWriter(fileWriter);
        writer.write(document);
        writer.close();
        fileWriter.close();
    }

方法三:迭代器遍历

 //1.创建Reader对象
        SAXReader reader = new SAXReader();
        //2.加载xml
        Document document = reader.read(new File(pomXml));
        //3.获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();

问题:

  • getText()getStringValue() 区别:
    getText()返回当前节点文本内容 ,,getStringValue()返回当前节点的子孙节点所有文本内容连接成的字符串
    https://blog.csdn.net/qq_35873847/article/details/77711559
原网站

版权声明
本文为[wfsm]所创,转载请带上原文链接,感谢
https://waterkid.blog.csdn.net/article/details/125047834