Class XPathQuery
java.lang.Object
cloud.opencode.base.xml.xpath.XPathQuery
XPath Query - Builder for complex XPath queries
XPath 查询 - 复杂 XPath 查询的构建器
This class provides a fluent interface for building and executing XPath queries with support for namespaces and variables.
此类提供流式接口,用于构建和执行支持命名空间和变量的 XPath 查询。
Usage Examples | 使用示例:
// Simple query
String name = XPathQuery.create()
.xml("<root><name>John</name></root>")
.selectString("//name/text()");
// Query with namespace
String value = XPathQuery.create()
.xml(nsXml)
.namespace("ns", "http://example.com")
.selectString("//ns:item/text()");
// Query with variables
String result = XPathQuery.create()
.xml(xml)
.variable("userId", "123")
.selectString("//user[@id=$userId]/name");
Features | 主要功能:
- Fluent builder for complex XPath queries - 复杂 XPath 查询的流式构建器
- Namespace and variable support - 命名空间和变量支持
- Multiple result type selection (string, number, node list) - 多种结果类型选择(字符串、数字、节点列表)
Security | 安全性:
- Thread-safe: No (mutable builder) - 线程安全: 否(可变构建器)
- Null-safe: No (throws on null XML/expression) - 空值安全: 否(null XML/表达式抛异常)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic XPathQuerycreate()Creates a new XPath query.Sets the Document to query.Adds a namespace binding.namespaces(Map<String, String> namespaces) Adds multiple namespace bindings.selectBoolean(String xpath) Selects a boolean value.selectElement(String xpath) Selects a single element.selectElements(String xpath) Selects multiple elements.selectNumber(String xpath) Selects a number value.selectString(String xpath) Selects a string value.selectValues(String xpath) Selects string values.Adds a variable binding.Sets the XML to query.
-
Method Details
-
create
Creates a new XPath query. 创建新的 XPath 查询。- Returns:
- the query object | 查询对象
-
xml
Sets the XML to query. 设置要查询的 XML。- Parameters:
xml- the XML string | XML 字符串- Returns:
- this query for chaining | 此查询以便链式调用
-
document
Sets the Document to query. 设置要查询的 Document。- Parameters:
document- the Document | Document- Returns:
- this query for chaining | 此查询以便链式调用
-
namespace
Adds a namespace binding. 添加命名空间绑定。- Parameters:
prefix- the namespace prefix | 命名空间前缀namespaceUri- the namespace URI | 命名空间 URI- Returns:
- this query for chaining | 此查询以便链式调用
-
namespaces
Adds multiple namespace bindings. 添加多个命名空间绑定。- Parameters:
namespaces- the namespace map (prefix -> URI) | 命名空间映射(前缀 -> URI)- Returns:
- this query for chaining | 此查询以便链式调用
-
variable
Adds a variable binding. 添加变量绑定。- Parameters:
name- the variable name | 变量名value- the variable value | 变量值- Returns:
- this query for chaining | 此查询以便链式调用
-
selectString
-
selectNumber
-
selectBoolean
-
selectElement
Selects a single element. 选择单个元素。- Parameters:
xpath- the XPath expression | XPath 表达式- Returns:
- the element, or null if not found | 元素,如果未找到则返回 null
-
selectElements
Selects multiple elements. 选择多个元素。- Parameters:
xpath- the XPath expression | XPath 表达式- Returns:
- the list of elements | 元素列表
-
selectValues
-