Class OpenXPath
java.lang.Object
cloud.opencode.base.xml.xpath.OpenXPath
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 Summary
Modifier and TypeMethodDescriptionstatic intCounts nodes matching the XPath.static booleanChecks if a node matching the XPath exists.static BooleanselectBoolean(String xml, String xpath) Selects a boolean using XPath.static BooleanselectBoolean(Document document, String xpath) Selects a boolean from a Document.static XmlElementselectElement(String xml, String xpath) Selects a single XmlElement using XPath.static XmlElementselectElement(Document document, String xpath) Selects a single XmlElement from a Document.static XmlElementselectElement(Node node, String xpath) Selects a single XmlElement from a Node.static List<XmlElement> selectElements(String xml, String xpath) Selects XmlElements using XPath.static List<XmlElement> selectElements(Document document, String xpath) Selects XmlElements from a Document.static List<XmlElement> selectElements(Node node, String xpath) Selects XmlElements from a Node.static NodeselectNode(String xml, String xpath) Selects a single Node using XPath.static NodeselectNode(Document document, String xpath) Selects a single Node from a Document.static NodeselectNode(Node node, String xpath) Selects a single Node from a Node.static NodeListselectNodes(String xml, String xpath) Selects NodeList using XPath.static NodeListselectNodes(Document document, String xpath) Selects NodeList from a Document.static NodeListselectNodes(Node node, String xpath) Selects NodeList from a Node.static NumberselectNumber(String xml, String xpath) Selects a number using XPath.static NumberselectNumber(Document document, String xpath) Selects a number from a Document.static StringselectString(String xml, String xpath) Selects a string value using XPath.static StringselectString(Document document, String xpath) Selects a string value from a Document.static StringSelects a string value from a Document with namespace mappings.static StringselectString(Node node, String xpath) Selects a string value from a Node.static StringSelects a string value from a Node with namespace mappings.static XPathQuerywithNamespaces(Map<String, String> namespaces) Creates an XPath query with namespaces.
-
Method Details
-
selectString
-
selectString
-
selectString
-
selectString
Selects a string value from a Document with namespace mappings. 使用命名空间映射从 Document 选择字符串值。- Parameters:
document- the Document | Documentxpath- the XPath expression | XPath 表达式namespaces- the namespace mappings (prefix -> URI) | 命名空间映射(前缀 -> URI)- Returns:
- the result string | 结果字符串
-
selectString
Selects a string value from a Node with namespace mappings. 使用命名空间映射从 Node 选择字符串值。- Parameters:
node- the Node | Nodexpath- the XPath expression | XPath 表达式namespaces- the namespace mappings (prefix -> URI) | 命名空间映射(前缀 -> URI)- Returns:
- the result string | 结果字符串
-
selectNumber
-
selectNumber
-
selectBoolean
-
selectBoolean
-
selectNode
-
selectNode
-
selectNode
-
selectNodes
-
selectNodes
-
selectNodes
-
selectElement
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
Selects a single XmlElement from a Document. 从 Document 选择单个 XmlElement。- Parameters:
document- the Document | Documentxpath- the XPath expression | XPath 表达式- Returns:
- the result element, or null if not found | 结果元素,如果未找到则返回 null
-
selectElement
Selects a single XmlElement from a Node. 从 Node 选择单个 XmlElement。- Parameters:
node- the Node | Nodexpath- the XPath expression | XPath 表达式- Returns:
- the result element, or null if not found | 结果元素,如果未找到则返回 null
-
selectElements
Selects XmlElements using XPath. 使用 XPath 选择 XmlElement 列表。- Parameters:
xml- the XML string | XML 字符串xpath- the XPath expression | XPath 表达式- Returns:
- the list of elements | 元素列表
-
selectElements
Selects XmlElements from a Document. 从 Document 选择 XmlElement 列表。- Parameters:
document- the Document | Documentxpath- the XPath expression | XPath 表达式- Returns:
- the list of elements | 元素列表
-
selectElements
Selects XmlElements from a Node. 从 Node 选择 XmlElement 列表。- Parameters:
node- the Node | Nodexpath- the XPath expression | XPath 表达式- Returns:
- the list of elements | 元素列表
-
exists
-
count
-
withNamespaces
Creates an XPath query with namespaces. 创建带命名空间的 XPath 查询。- Parameters:
namespaces- the namespace map (prefix -> URI) | 命名空间映射(前缀 -> URI)- Returns:
- the XPath query object | XPath 查询对象
-