码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • XMLPath的基本使用


    本文最开始发表于择维士社区

    文章目录

    • 什么是XPath
    • XPath基本格式
      • XPath的表达式
      • 更多的预测格式
    • XPath在Java中的使用示例
      • 获取一堆节点
      • 根据某个id获取节点:
      • 根据某个tag获取节点:
    • 参考

    什么是XPath

    XPath是一种用于在xml格式的内容中提取信息的方式. 它与从JSON中提取信息的JSONPath类似. (如何使用JSONPath). 本文将介绍xpath的基本格式以及在Java中如何使用Xpath提取信息.

    XPath基本格式

    XPath的表达式

    一般表达式格式如下: /foo/bar 可以搜索如下的xml内容/节点:

    <foo>
         <bar/>
    foo>
    或者:
    <foo>
         <bar/>
         <bar/>
         <bar/>
    foo>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    如果以// 开始代表忽略深度限制.

    常见的节点元素类型:

    Location PathDescription
    /foo/bar/@idbar元素的id属性
    /foo/bar/text()bar元素的text值.

    预测允许我们来查找满足条件的节点. 格式是[表达式]. 比如:

    选择所有foo节点(含所有子节点,孙子节点...)包含include属性,且值为true
    //foo[@include='true']
    
    //foo[@include='true'][@mode='bar']
    
    • 1
    • 2
    • 3
    • 4

    更多的预测格式

    
    <Tutorials>
        <Tutorial tutId="01" type="java">
            <title>Guavatitle>
            <description>Introduction to Guavadescription>
            <date>04/04/2016date>
            <author>GuavaAuthorauthor>
        Tutorial>
        <Tutorial tutId="02" type="java">
            <title>XMLtitle>
            <description>Introduction to XPathdescription>
            <date>04/05/2016date>
            <author>XMLAuthorauthor>
        Tutorial>
    Tutorials>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    比如上面的例子:

    /Tutorials/Tutorial[1]
    
    /Tutorials/Tutorial[first()]
    
    /Tutorials/Tutorial[position()<4]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    XPath在Java中的使用示例

    JDK11中原生支持了xmlpath解析, 以解析上面的xml为例:

    获取一堆节点

    返回所有 /Tutorials/Tutorial 节点:

    import org.w3c.dom.*;
    
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    import java.io.*;
    
    public class XmlDemo {
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document xmlDocument = builder.parse(new ByteArrayInputStream(EXAMPLE_STRING.getBytes()));
            XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = "/Tutorials/Tutorial";
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            System.out.println("Find nodes length = " + nodeList.getLength());
        }
    
    
    
    
        static String EXAMPLE_STRING = "" +
                "\n" +
                "    \n" +
                "        Guava\n" +
                "  Introduction to Guava\n" +
                "  04/04/2016\n" +
                "  GuavaAuthor\n" +
                "    \n" +
                "    \n" +
                "        XML\n" +
                "  Introduction to XPath\n" +
                "  04/05/2016\n" +
                "  XMLAuthor\n" +
                "    \n" +
                "";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    根据某个id获取节点:

    获取Tutorial (tutId=01)的节点:

            String expression = "/Tutorials/Tutorial[@tutId=\"01\"]";
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            System.out.println("Find node" + node);
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    根据某个tag获取节点:

    获取包含title的节点 以及节点值为Guava:

            String expression = "//Tutorial[descendant::title[text()=" + "'" + "Guava" + "'" + "]]";
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            System.out.println("Found title=Guava length:" + nodeList.getLength());
    
    • 1
    • 2
    • 3

    参考

    1.JDK中的api

    2.在线测试xmlpath
    在这里插入图片描述

  • 相关阅读:
    AI与Prompt:解锁软件开发团队的魔法咒语,在复杂任务上生成正确率更高的代码
    SBT20100VDC-ASEMI超低压降、低功耗肖特基SBT20100VDC
    JDBC数据库连接池
    SpringBoot
    硬核实力!飞凌 TI Sitara AM62X 系列-335x经典再续
    SpringBoot项目使用jasypt加解密
    文件夹怎么隐藏?电脑重要文件夹隐藏的方法
    RabbitMQ之单机多实例部署
    2024年天津财经大学珠江学院专升本预计新增金融学招生专业
    java计算机毕业设计酒店后厨供应商订单合并系统源码+数据库+lw文档+系统
  • 原文地址:https://blog.csdn.net/scugxl/article/details/127834768
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号