Java操作XML(6)--使用dom4j处理XML

    dom4j是一个用于处理XML的开源框架,该框架集成了XPath,完全支持DOM,SAX,JAXP。本文主要介绍使用dom4j来处理XML,文中所使用到的软件版本:Java 1.8.0_191、dom4j 2.1.3、jaxen 1.2.0。

1、前言

    dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点。DOM4J性能很好,连Sun的JAXM也在用DOM4J。目前许多开源项目中大量采用DOM4J,例如Hibernate使用DOM4J来读取XML配置文件。

2、使用dom4j操作XML

2.1、XML文件

<?xml version="1.0" encoding="utf-8" ?>
<school:grade xmlns:school="http://www.w3.org/TR/html4/school/">
    <school:student rollno="1" school:age="10">
        <school:firstname>cxx1</school:firstname>
        <lastname>Bob1</lastname>
        <nickname>stars1</nickname>
        <marks>85</marks>
    </school:student>
    <student rollno="2">
        <firstname>cxx2</firstname>
        <lastname>Bob2</lastname>
        <nickname>stars2</nickname>
        <marks>85</marks>
    </student>
    <student rollno="3">
        <firstname>cxx3</firstname>
        <lastname>Bob3</lastname>
        <nickname>stars3</nickname>
        <marks>85</marks>
    </student>
</school:grade>

2.2、pom.xml

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.2.0</version>
</dependency>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

2.3、Java代码例子

该例子演示了使用dom4j解析XML、dom4j中XPath的使用以及使用dom4j来生成XML。

package com.abc.demo.general.xml;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * 使用dom4j处理xml
 */
public class Dom4jCase {
    private static Logger logger = LoggerFactory.getLogger(Dom4jCase.class);

    /**
     * 解析xml
     * @throws Exception
     */
    @Test
    public void parse() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(Dom4jCase.class.getResourceAsStream("student.xml"));//student.xml和Dom4jCase.java位于同一目录
        //Document document = DocumentHelper.parseText(xmlString);//解析字符串
        Element root = document.getRootElement();
        List<Element> list = root.elements("student");
        for (int i = 0; i < list.size(); i++) {
            Element student = list.get(i);
            logger.info("学生编号{}", student.attributeValue("rollno"));
            logger.info("age:{}", student.attributeValue("age"));
            logger.info("firstname:{}", student.elementText("firstname"));
            logger.info("lastname:{}", student.elementText("lastname"));
            logger.info("nickname:{}", student.elementText("nickname"));
            logger.info("marks:{}", student.elementText("marks"));
        }
    }

    /**
     * xpath使用
     * @throws Exception
     */
    @Test
    public void xpath() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(Dom4jCase.class.getResourceAsStream("student.xml"));//student.xml和Dom4jCase.java位于同一目录
        logger.info("查找存在rollno属性的student节点,找到的节点再查找lastname子节点,找到的节点再查找文本节点");
        List<Node> list = document.selectNodes("//student[@rollno]/lastname/text()");//第二、三位学生
//        List<Node> list = document.selectNodes("//student[@rollno]/child::lastname/text()");//效果同上
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            logger.info(node.getText());
        }

        logger.info("查找存在rollno属性的school:student节点");
        list = document.selectNodes("//school:student[@rollno]");//第一位学生
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            //获取rollno的属性值
            logger.info(node.valueOf("@rollno"));
        }
    }

    /**
     * 生成xml
     */
    @Test
    public void toXml() throws Exception {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("school:grade", "http://www.w3.org/TR/html4/school/");
        //第一个学生
        Element student = root.addElement("school:student");
        student.addAttribute("rollno", "1");
        student.addAttribute("school:age", "10");
        student.addElement("school:firstname").setText("cxx1");
        student.addElement("lastname").setText("Bob1");
        student.addElement("nickname").setText("stars1");
        student.addElement("marks").setText("85");
        //第二个学生
        student = root.addElement("student");
        student.addAttribute("rollno", "2");
        student.addElement("school:firstname").setText("cxx2");
        student.addElement("lastname").setText("Bob2");
        student.addElement("nickname").setText("stars2");
        student.addElement("marks").setText("85");
        //第三个学生
        student = root.addElement("student");
        student.addAttribute("rollno", "3");
        student.addElement("school:firstname").setText("cxx3");
        student.addElement("lastname").setText("Bob3");
        student.addElement("nickname").setText("stars3");
        student.addElement("marks").setText("85");

        //直接转成字符串
//        logger.info(document.asXML());

        //格式化并输出到输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        logger.info(out.toString());
    }
}

 

posted @ 2020-12-26 15:35  且行且码  阅读(452)  评论(0编辑  收藏  举报