springboot中使用xpath
XPathExpression
参考:
https://docs.spring.io/spring-ws/site/apidocs/org/springframework/xml/xpath/XPathExpression.html
XPath
XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的
<?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 | 选取根元素 bookstore。注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径! |
bookstore/book | 选取属于 bookstore 的子元素的所有 book 元素。 |
//book | 选取所有 book 子元素,而不管它们在文档中的位置。 |
bookstore//book | 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。 |
//@lang | 选取名为 lang 的所有属性。 |
java使用xpath
package com.example.demo.xml.xpath;import java.io.File;
import java.io.FileInputStream;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;/*** XPathTest** @author huangchen* @since 2022/9/2*/
public class XPathTest {private static Document doc;private static XPath xpath;public static void main(String[] args) throws Exception {// 初始化Document、XPath对象init();// 获取根元素getRootEle(); // rss--------null// 获取子元素并打印getChildEles(); // title language item item// start 不是很重要getPartEles();haveChildsEles();getLevelEles();getAttrEles();// end 不是很重要// 任意位置的title: title->Java Tutorials and Examples 2->null 任意位置的title: title->Java Tutorials 2->null 任意位置的title: title->Java Examples 2->null 任意位置的title: title->Harry Potter->null 任意位置的title: title->Learning XML->null 任意位置的title: title->t->null 任意位置的title: title->tt->null NodeList nodeList1 = (NodeList)xpath.evaluate("//title", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList1.getLength(); i++) {System.out.print("任意位置的title: " + nodeList1.item(i).getNodeName() + "->" + nodeList1.item(i).getTextContent()+ "->" + nodeList1.item(i).getNodeValue() + " ");}System.out.println();// a下面的title: title->tt->nullNodeList nodeList2 = (NodeList)xpath.evaluate("/rss/a/title", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList2.getLength(); i++) {System.out.print("a下面的title: " + nodeList2.item(i).getNodeName() + "->" + nodeList2.item(i).getTextContent()+ "->" + nodeList2.item(i).getNodeValue() + " ");}System.out.println();// a下面的任意位置的title: title->t->null a下面的任意位置的title: title->tt->null NodeList nodeList22 = (NodeList)xpath.evaluate("/rss/a//title", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList22.getLength(); i++) {System.out.print("a下面的任意位置的title: " + nodeList22.item(i).getNodeName() + "->" + nodeList22.item(i).getTextContent()+ "->" + nodeList22.item(i).getNodeValue() + " ");}System.out.println();NodeList nodeList3 = (NodeList)xpath.evaluate("/rss/channel//title", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList3.getLength(); i++) {System.out.print("channel下的任意位置的title: " + nodeList3.item(i).getNodeName() + "->" + nodeList3.item(i).getTextContent()+ "->" + nodeList3.item(i).getNodeValue() + " ");}System.out.println();System.out.println("打印根节点下的所有元素节点");System.out.println("有子元素的 节点的数量" + doc.getDocumentElement().getChildNodes().getLength());NodeList nodeList = doc.getDocumentElement().getChildNodes();for (int i = 0; i < nodeList.getLength(); i++) {if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {System.out.print(nodeList.item(i).getNodeName() + " ");}}}/*** 初始化Document、XPath对象** @throws Exception*/public static void init() throws Exception {// 创建Document对象DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setValidating(false);DocumentBuilder db = dbf.newDocumentBuilder();doc = db.parse(new FileInputStream(new File("D:\\tools\\IDEA\\idea-project\\springboot\\demo\\spring-boot-demo-mybaits-plus\\src\\main\\resources\\demo.xml")));// 创建XPath对象XPathFactory factory = XPathFactory.newInstance();xpath = factory.newXPath();}// 获取根元素// 表达式可以更换为/*,/rsspublic static void getRootEle() throws XPathExpressionException {Node node = (Node)xpath.evaluate("/rss", doc, XPathConstants.NODE);System.out.println(node.getNodeName() + "--------" + node.getNodeValue());}// 获取子元素并打印public static void getChildEles() throws XPathExpressionException {System.out.println("获取子元素并打印");NodeList nodeList = (NodeList)xpath.evaluate("/rss/channel/*", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList.getLength(); i++) {System.out.print(nodeList.item(i).getNodeName() + " ");}System.out.println();}// 获取部分元素// 只获取元素名称为title的元素public static void getPartEles() throws XPathExpressionException {System.out.println("只获取元素名称为title的元素");NodeList nodeList = (NodeList)xpath.evaluate("//*[name() = 'title']", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList.getLength(); i++) {System.out.print(nodeList.item(i).getNodeName() + "-->" + nodeList.item(i).getTextContent());}System.out.println();}// 获取包含子节点的元素public static void haveChildsEles() throws XPathExpressionException {System.out.println("获取包含子节点的元素");NodeList nodeList = (NodeList)xpath.evaluate("//*[*]", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList.getLength(); i++) {System.out.print(nodeList.item(i).getNodeName() + " ");}System.out.println();}// 获取指定层级的元素public static void getLevelEles() throws XPathExpressionException {System.out.println("获取指定层级的元素");NodeList nodeList = (NodeList)xpath.evaluate("/*/*/*/*", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList.getLength(); i++) {System.out.print(nodeList.item(i).getNodeName() + "-->" + nodeList.item(i).getTextContent() + " ");}System.out.println("-----------------------------");}// 获取指定属性的元素// 获取所有大于指定价格的书箱public static void getAttrEles() throws XPathExpressionException {System.out.println("获取指定属性的元素");NodeList nodeList =(NodeList)xpath.evaluate("//bookstore/book[price>35.00]/title", doc, XPathConstants.NODESET);for (int i = 0; i < nodeList.getLength(); i++) {System.out.print(nodeList.item(i).getNodeName() + "-->" + nodeList.item(i).getTextContent() + " ");}System.out.println();}
}
demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><title>Java Tutorials and Examples 2</title><language>en-us</language><item><title><![CDATA[Java Tutorials 2]]></title><link>http://www.javacodegeeks.com/</link></item><item><title><![CDATA[Java Examples 2]]></title><link>http://examples.javacodegeeks.com/</link></item></channel><college name="c1"><class name="class1"><student name="stu1" sex='male' age="21"/><student name="stu2" sex='female' age="20"/><student name="stu3" sex='female' age="20"/></class></college><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><a><b><title>t</title></b><title>tt</title></a>
</rss>
spring中使用
XPathExpression
package org.springframework.xml.xpath;import java.util.List;
import org.w3c.dom.Node;public interface XPathExpression {boolean evaluateAsBoolean(Node var1) throws XPathException;Node evaluateAsNode(Node var1) throws XPathException;List<Node> evaluateAsNodeList(Node var1) throws XPathException;double evaluateAsNumber(Node var1) throws XPathException;String evaluateAsString(Node var1) throws XPathException;<T> T evaluateAsObject(Node var1, NodeMapper<T> var2) throws XPathException;<T> List<T> evaluate(Node var1, NodeMapper<T> var2) throws XPathException;
}
springboot中使用xpath相关推荐
- 在SpringBoot中使用Spring Session解决分布式会话共享问题
在SpringBoot中使用Spring Session解决分布式会话共享问题 问题描述: 每次当重启服务器时,都会导致会员平台中已登录的用户掉线.这是因为每个用户的会话信息及状态都是由session ...
- SpringBoot 中 JPA 的使用
前言 第一次使用 Spring JPA 的时候,感觉这东西简直就是神器,几乎不需要写什么关于数据库访问的代码一个基本的 CURD 的功能就出来了.下面我们就用一个例子来讲述以下 JPA 使用的基本操作 ...
- 难以想象SpringBoot中的条件注解底层居然是这样实现的
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | https://urlify.cn/bm2qqi Spr ...
- 面试:SpringBoot中的条件注解底层是如何实现的?
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | https://urlify.cn/bm2qqi Spr ...
- springboot yml怎么建常量_【Java】SpringBoot 中从application.yml中获取自定义常量
由于这里我想通过java连接linux,connection连接需要host.port.username.password及其他路径等等.不想每次修改的时候都去改源文件,所以想写在applicatio ...
- Springboot中给图片添加文字水印
Springboot中给图片添加文字水印 工作中遇到给图片添加文字水印的需求,记录下来方便之后查阅 需求内容: 给一张图片添加指定文字水印,使一张图片上有多个水印内容,并且设定一个水印开关,可指定是否 ...
- 你知道如何在springboot中使用redis吗
特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce ...
- WebSocket的故事(六)—— Springboot中,实现更灵活的WebSocket
概述 WebSocket的故事系列计划分五大篇六章,旨在由浅入深的介绍WebSocket以及在Springboot中如何快速构建和使用WebSocket提供的能力.本系列计划包含如下几篇文章: 第一篇 ...
- 在Java中使用xpath对xml解析
个人博客地址:https://www.vastyun.com xpath是一门在xml文档中查找信息的语言.xpath用于在XML文档中通过元素和属性进行导航.它的返回值可能是节点,节点集合,文本, ...
最新文章
- 查看源代码Source not found及在eclipse中配置jdk的src.zip源代码
- 前端一HTML:七:css初步认识
- 页面引用CSS和Javascript时,内联和外置的区别
- JQUERY获取各种HTML控件的值
- collect2: error: ld returned 1 exit status编译错误
- 《Java8实战》笔记(10):用Optional取代null
- 【算法分析与设计】浅析二分查找
- java插入排序实现,经典(Java版)排序算法的分析及实现之一直接插入排序
- Delphi非应用程序主窗口创建MDI
- 28.Linux/Unix 系统编程手册(上) -- 详述进程创建和程序执行
- springmvc中@PathVariable和@RequestParam的区别(百度收集)
- 单片机c语言跑马灯实验报告,单片机跑马灯实验报告
- 化学能推进永远无法实现外星旅行
- ios定制中间突出的tabBar
- 深度学习教程(12) | CNN应用:目标检测(吴恩达·完整版)
- 智能爆炸的真实(上)
- js将汉字转为相应的拼音
- 【时间序列】时间序列基本概念总结
- 刷了一个半月算法题,我薪资终于Double了
- 使用CainAbel进行网络嗅探