跳转到主要内容

这是本节的多页打印视图。 .

返回本页常规视图.

XML 处理

Dom4j 与 XPath

共 2 篇文档。

1 - Dom4j 入门笔记

Maven 引入依赖包

<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.4</version>
</dependency>
<!-- xpath  -->
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>2.0.0</version>
</dependency>
<!-- 测试 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.8.1</version>
</dependency>

创建 XML

package com.github.vianlex.xml.dom4j;

import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
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.jupiter.api.Test;

public class Dom4jTest {

    /**
	 * 构建 xml
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <beans>
	 * 		<bean id="userService" class="com.github.vianlex.service.impl.UserServiceImpl">
	 * 			<property name="userDao" ref="userDao"/>
	 *      </bean>
	 *      <bean id="roleService" class="com.github.vianlex.service.impl.RoleServiceImpl">
	 *      	<property name="roleDao" ref="roleDao"></property>
	 *      </bean>
	 *      <bean id="roleDao" class="com.github.vianlex.dao.RoleDao"></bean>
	 *      <bean id="userDao" class="com.github.vianlex.dao.UserDao"></bean>
	 * <beans>
	 *
	 */
    @Test
	public void createXml() {

		Document document = DocumentHelper.createDocument();
		// 创建 <beans> 标签
		Element root = document.addElement("beans");
		// 创建 <beans> 的子标签 <bean>
		Element userServiceBean = root.addElement("bean").addAttribute("id", "userService").addAttribute("class",
				"com.github.vianlex.service.impl.UserServiceImpl");
		// 创建 <bean> 的子标签 <property>
		userServiceBean.addElement("property").addAttribute("name", "userDao").addAttribute("ref", "userDao");

		Element roleServiceBean = root.addElement("bean").addAttribute("id", "roleService").addAttribute("class",
				"com.github.vianlex.service.impl.RoleServiceImpl");
		roleServiceBean.addElement("property").addAttribute("name", "roleDao").addAttribute("ref", "roleDao");

		Element userDao = root.addElement("bean").addAttribute("id", "roleDao").addAttribute("class",
				"com.github.vianlex.dao.RoleDao");

		Element roleDao = root.addElement("bean").addAttribute("id", "userDao").addAttribute("class",
				"com.github.vianlex.dao.UserDao");

		System.out.println(document.asXML());

		System.out.println(" ============================================== ");
		// 美化打印 xml
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter writer = new XMLWriter(System.out, format);
			writer.write(document);

		} catch (Exception e) {
			// TODO: handle exception
		}

		System.out.println(" ============================================== ");

		// 紧凑型打印 xml
		try {
			// Compact format to System.out
			OutputFormat format = OutputFormat.createCompactFormat();
			XMLWriter writer = new XMLWriter(System.out, format);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
}

将文本 xml 写文件

FileWriter out = new FileWriter("foo.xml");
document.write(out);
out.close();

解析 XML

  1. 解析文本 xml
@Test
public void getDocumentByText() {
    String xml = "<beans><bean id=\"userService\" class=\"com.github.vianlex.service.impl.UserServiceImpl\"><property name=\"userDao\" ref=\"userDao\"/></bean><bean id=\"roleService\" class=\"com.github.vianlex.service.impl.RoleServiceImpl\"><property name=\"roleDao\" ref=\"roleDao\"/></bean><bean id=\"roleDao\" class=\"com.github.vianlex.dao.RoleDao\"/><bean id=\"userDao\" class=\"com.github.vianlex.dao.UserDao\"/></beans>";
    try {
        Document document =  DocumentHelper.parseText(xml);
        System.out.println(document.asXML());
    } catch (DocumentException e) {
        throw new RuntimeException("解析文本 xml 出错", e);
    }
}
  1. 解析文件中的 xml
@Test
public void getDocumentByXmlFile() {
    String fileName = "spring-context.xml";
    SAXReader saxReader = new SAXReader();
    try {
        Document document = saxReader.read(this.getClass().getResourceAsStream(fileName));
        System.out.println(document.asXML());
    } catch (DocumentException e) {
        throw new RuntimeException("解析 xml 文件出错", e);
    }

}

解析 document


/**
	 * 迭代方式解析 document
	 */
@Test
public void parseDocumentByIter() throws Exception{

    String xml = "<beans id=\"helloRoot\"><bean id=\"userService\" class=\"com.github.vianlex.service.impl.UserServiceImpl\"><property name=\"userDao\" ref=\"userDao\"/></bean><bean id=\"roleService\" class=\"com.github.vianlex.service.impl.RoleServiceImpl\"><property name=\"roleDao\" ref=\"roleDao\"/></bean><bean id=\"roleDao\" class=\"com.github.vianlex.dao.RoleDao\"/><bean id=\"userDao\" class=\"com.github.vianlex.dao.UserDao\"/></beans>";
    Document document =  DocumentHelper.parseText(xml);

    // root elements
    Element root = document.getRootElement();

    // iterate through child elements of root
    for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
        Element element = it.next();
        System.out.println("元素标签名:"+element.getName()+",标签属性:(id = '"+element.attributeValue("id") +"', class='"+element.attributeValue("class")+"')" );
    }

    System.out.println(" ================================================= ");
    // iterate through child elements of root with element name "bean"
    for (Iterator<Element> it = root.elementIterator("bean"); it.hasNext();) {
        Element element = it.next();
        System.out.println("元素标签名:"+element.getName()+",标签属性:(id = '"+element.attributeValue("id") +"', class='"+element.attributeValue("class")+"')" );
    }

    System.out.println(" ====================================== ");
    // iterate through attributes of root
    for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext();) {
        Attribute attribute = it.next();
        System.out.println(attribute.getName() + " = " + attribute.getValue());
    }
}



/**
    * xpath 方式解析 document
    * @throws Exception
    */
@Test
public void parseDocumentByXpath() throws Exception {

    String xml = "<beans><bean id=\"userService\" class=\"com.github.vianlex.service.impl.UserServiceImpl\"><property name=\"userDao\" ref=\"userDao\"/></bean><bean id=\"roleService\" class=\"com.github.vianlex.service.impl.RoleServiceImpl\"><property name=\"roleDao\" ref=\"roleDao\"/></bean><bean id=\"roleDao\" class=\"com.github.vianlex.dao.RoleDao\"/><bean id=\"userDao\" class=\"com.github.vianlex.dao.UserDao\"/></beans>";
    Document document =  DocumentHelper.parseText(xml);
    // 查找 <bean> 元素
    List<Node> beanNodeList = document.selectNodes("//bean");
    for (Node node : beanNodeList) {
        Element element = (Element) node;
        System.out.println(element.getName()+ ", " + element.attributeValue("id") + element.attributeValue("class"));
    }
    // 查找 <property> 元素
    List<Node> propertyNodeList = document.selectNodes("//property");
    for (Node node : propertyNodeList) {
        Element element = (Element) node;
        System.out.println(element.getName()+ ", " + element.attributeValue("name") + element.attributeValue("ref"));
    }

}

参考链接

  1. https://dom4j.github.io/#top

2 - XPath 笔记

XPath 节点术语

在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。树的根被称为文档节点或者根节点。

节点关系

  • 父(Parent), 每个元素以及属性都有一个父节点。
  • 子(Children), 元素节点可有零个、一个或多个子节点。
  • 同胞(Sibling),拥有相同的父的节点。
  • 先辈(Ancestor),某节点的父节点、及父父节点,等等。
  • 后代(Descendant),某个节点的子节点,及子子节点,等等。

XPath 语法

示例语法 XML 实例文档,如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
    <book>
        <title lang="eng">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book>
        <title lang="eng">Learning XML</title>
        <price>39.95</price>
    </book>
</bookstore>

选取节点

表达式描述
nodename选取此节点的所有子节点。
/表示文档树的起始
//匹配文档中的任何节点,而不考虑它们的位置。
.选取当前节点。
..选取当前节点的父节点。
@选取属性。

使用例子:

# 获取文档中的 bookstore 节点
/bookstore

# 获取文档 bookstore 节点下的 book 节点
/bookstore/book

# 获取文档中的所有 book 节点
//book

# 获取文档中的所有 title 节点
//title

# 获取名为 lang 的所有属性
//@lang

谓语选择

谓语用来查找某个特定的节点或者包含某个指定的值的节点,被嵌在方括号中使用。

/bookstore/book[1]选取属于 bookstore 子元素的第一个 book 元素。
/bookstore/book[last()]选取属于 bookstore 子元素的最后一个 book 元素。
/bookstore/book[last()-1]选取属于 bookstore 子元素的倒数第二个 book 元素。
/bookstore/book[position()<3]选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
//title[@lang]选取所有拥有名为 lang 的属性的 title 元素。
//title[@lang=‘eng’]选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
/bookstore/book[price>35.00]选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。
/bookstore/book[price>35.00]/title选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。

选取未知节点

XPath 通配符可用来选取未知的 XML 元素。

通配符描述
*匹配任何元素节点。如 //book/*,选取 book 节点下面的所有节点,如 //*,选择文档中的所有元素。
@*匹配任何属性节点。如 //title[@*] 选取所有带有属性的 title 元素
node()匹配任何类型的节点。如 /bookstore/node() 选取 bookstore 节点下的任何节点

选取若干路径

通过在路径表达式中使用“|”运算符,您可以选取若干个路径。

路径表达式 结果
//book/title//book/price 选取 book 元素的所有 title 和 price 元素。
//title//price 选取文档中的所有 title 和 price 元素。
/bookstore/book/title//price 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。

XPath 轴

轴可定义相对于当前节点的节点集。

轴名称结果
ancestor 选取当前节点的所有先辈(父、祖父等)。
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。
attribute 选取当前节点的所有属性。
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
namespace 选取当前节点的所有命名空间节点。
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。

参考链接

  1. https://www.w3school.com.cn/xpath/index.asp
  2. 在线测试地址