使用dom4j解析xml

Posted by Xsp on September 10, 2018

配置

dom4j 使用 jaxen 来解析 xpath,如果不加上的话,在使用 document.selectNodes 解析xpath的时候会报错 java.lang.NoClassDefFoundError: org/jaxen/JaxenException

<dependencies>
    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.6</version>
    </dependency>
</dependencies>

demo

package xml;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class Dom4jDemo {
    public static void main(String[] args) {
        Dom4jDemo dom4jDemo = new Dom4jDemo();
        dom4jDemo.parserXml("S_1.xml");
        dom4jDemo.createXml("hello.xml");
    }

    // https://www.tutorialspoint.com/java_xml/java_dom4j_create_document.htm
    public void createXml(String fileName) {
        try {
            Document document = DocumentHelper.createDocument();

            Element root = document.addElement("cars");
            Element supercarElement = root.addElement("supercars").addAttribute("company", "Ferrai");
            supercarElement.addElement("carname").addAttribute("type", "Ferrari 101").addText("Ferrari 101");

            // Pretty print the document to System.out
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            writer = new XMLWriter(System.out, format);
            writer.write(document);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void parserXml(String fileName) {

        try {
            SAXReader saxReader = new SAXReader();

            Document document = saxReader.read(new File(fileName));

            Node rootElement = document.getRootElement();
            // 获取节点名
            System.out.println(rootElement.getName());

            // 使用 xpath 来选择节点
            // xpath 语法 https://www.tutorialspoint.com/java_xml/java_xpath_parser.htm
            List<Node> nodeList = document.selectNodes("/hierarchy/node/node/node/node/node");

            // 获取属性值,可以转型为 Element,这里 Element 接口继承自 Node 接口
            // Node 节点不仅仅是Element,也可以是 文本节点
            Node node = nodeList.get(0);
            Element element = (Element) node;

            System.out.println(element.attributeValue("resource-id"));
            // 获取属性值,也可以直接用 xpath 获取
            System.out.println(node.valueOf("@resource-id"));

        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
    }
}

参考

  • https://www.tutorialspoint.com/java_xml/java_dom4j_parse_document.htm

  • https://dom4j.github.io/