Class OpenXPath

java.lang.Object
cloud.opencode.base.xml.xpath.OpenXPath

public final class OpenXPath extends Object
XPath Entry Class - Static facade for XPath operations XPath 入口类 - XPath 操作的静态门面

This class provides static methods for evaluating XPath expressions against XML content in various forms.

此类提供静态方法,用于针对各种形式的 XML 内容求值 XPath 表达式。

Usage Examples | 使用示例:

// Simple string query
String name = OpenXPath.selectString(xml, "//user/name/text()");

// Query elements
List<XmlElement> users = OpenXPath.selectElements(xml, "//user");

// Query with namespace
String value = OpenXPath.withNamespaces(Map.of("ns", "http://example.com"))
    .selectString("//ns:item/text()");

// Check existence
boolean exists = OpenXPath.exists(xml, "//user[@id='1']");

// Count nodes
int count = OpenXPath.count(xml, "//user");

Features | 主要功能:

  • Static utilities for XPath evaluation - XPath 求值的静态工具
  • Namespace-aware XPath support - 支持命名空间的 XPath
  • Multiple return types (string, number, node list) - 多种返回类型(字符串、数字、节点列表)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • selectString

      public static String selectString(String xml, String xpath)
      Selects a string value using XPath. 使用 XPath 选择字符串值。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result string | 结果字符串
    • selectString

      public static String selectString(Document document, String xpath)
      Selects a string value from a Document. 从 Document 选择字符串值。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result string | 结果字符串
    • selectString

      public static String selectString(Node node, String xpath)
      Selects a string value from a Node. 从 Node 选择字符串值。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result string | 结果字符串
    • selectString

      public static String selectString(Document document, String xpath, Map<String,String> namespaces)
      Selects a string value from a Document with namespace mappings. 使用命名空间映射从 Document 选择字符串值。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      namespaces - the namespace mappings (prefix -> URI) | 命名空间映射(前缀 -> URI)
      Returns:
      the result string | 结果字符串
    • selectString

      public static String selectString(Node node, String xpath, Map<String,String> namespaces)
      Selects a string value from a Node with namespace mappings. 使用命名空间映射从 Node 选择字符串值。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      namespaces - the namespace mappings (prefix -> URI) | 命名空间映射(前缀 -> URI)
      Returns:
      the result string | 结果字符串
    • selectNumber

      public static Number selectNumber(String xml, String xpath)
      Selects a number using XPath. 使用 XPath 选择数字。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result number | 结果数字
    • selectNumber

      public static Number selectNumber(Document document, String xpath)
      Selects a number from a Document. 从 Document 选择数字。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result number | 结果数字
    • selectBoolean

      public static Boolean selectBoolean(String xml, String xpath)
      Selects a boolean using XPath. 使用 XPath 选择布尔值。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result boolean | 结果布尔值
    • selectBoolean

      public static Boolean selectBoolean(Document document, String xpath)
      Selects a boolean from a Document. 从 Document 选择布尔值。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result boolean | 结果布尔值
    • selectNode

      public static Node selectNode(String xml, String xpath)
      Selects a single Node using XPath. 使用 XPath 选择单个节点。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result Node | 结果节点
    • selectNode

      public static Node selectNode(Document document, String xpath)
      Selects a single Node from a Document. 从 Document 选择单个节点。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result Node | 结果节点
    • selectNode

      public static Node selectNode(Node node, String xpath)
      Selects a single Node from a Node. 从 Node 选择单个节点。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result Node | 结果节点
    • selectNodes

      public static NodeList selectNodes(String xml, String xpath)
      Selects NodeList using XPath. 使用 XPath 选择节点列表。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result NodeList | 结果节点列表
    • selectNodes

      public static NodeList selectNodes(Document document, String xpath)
      Selects NodeList from a Document. 从 Document 选择节点列表。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result NodeList | 结果节点列表
    • selectNodes

      public static NodeList selectNodes(Node node, String xpath)
      Selects NodeList from a Node. 从 Node 选择节点列表。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result NodeList | 结果节点列表
    • selectElement

      public static XmlElement selectElement(String xml, String xpath)
      Selects a single XmlElement using XPath. 使用 XPath 选择单个 XmlElement。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result element, or null if not found | 结果元素,如果未找到则返回 null
    • selectElement

      public static XmlElement selectElement(Document document, String xpath)
      Selects a single XmlElement from a Document. 从 Document 选择单个 XmlElement。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result element, or null if not found | 结果元素,如果未找到则返回 null
    • selectElement

      public static XmlElement selectElement(Node node, String xpath)
      Selects a single XmlElement from a Node. 从 Node 选择单个 XmlElement。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result element, or null if not found | 结果元素,如果未找到则返回 null
    • selectElements

      public static List<XmlElement> selectElements(String xml, String xpath)
      Selects XmlElements using XPath. 使用 XPath 选择 XmlElement 列表。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the list of elements | 元素列表
    • selectElements

      public static List<XmlElement> selectElements(Document document, String xpath)
      Selects XmlElements from a Document. 从 Document 选择 XmlElement 列表。
      Parameters:
      document - the Document | Document
      xpath - the XPath expression | XPath 表达式
      Returns:
      the list of elements | 元素列表
    • selectElements

      public static List<XmlElement> selectElements(Node node, String xpath)
      Selects XmlElements from a Node. 从 Node 选择 XmlElement 列表。
      Parameters:
      node - the Node | Node
      xpath - the XPath expression | XPath 表达式
      Returns:
      the list of elements | 元素列表
    • exists

      public static boolean exists(String xml, String xpath)
      Checks if a node matching the XPath exists. 检查是否存在匹配 XPath 的节点。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      true if exists | 如果存在则返回 true
    • count

      public static int count(String xml, String xpath)
      Counts nodes matching the XPath. 统计匹配 XPath 的节点数量。
      Parameters:
      xml - the XML string | XML 字符串
      xpath - the XPath expression | XPath 表达式
      Returns:
      the count | 数量
    • withNamespaces

      public static XPathQuery withNamespaces(Map<String,String> namespaces)
      Creates an XPath query with namespaces. 创建带命名空间的 XPath 查询。
      Parameters:
      namespaces - the namespace map (prefix -> URI) | 命名空间映射(前缀 -> URI)
      Returns:
      the XPath query object | XPath 查询对象