• XMLPath的基本使用


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

    什么是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
    在这里插入图片描述

  • 相关阅读:
    思维模型 正/反 木桶理论
    Git下载与安装
    深度适配云环境,火山引擎推出云操作系统veLinux
    蓝队追踪者工具TrackAttacker,以及免杀马生成工具
    使用 Vert.X Future/Promise 编写异步代码
    【MySQL】事务管理
    核心交换机、汇聚层交换机、接入层交换机的区别和特点
    Linux shell编程学习笔记9:字符串运算 和 if语句
    linux安装wget命令_linux下载文件到本地命令
    SSL证书对网站SEO的好处
  • 原文地址:https://blog.csdn.net/scugxl/article/details/127834768