Record Class XPathResult

java.lang.Object
java.lang.Record
cloud.opencode.base.xml.xpath.XPathResult
Record Components:
value - the raw result value | 原始结果值
elements - the list of element results | 元素结果列表
xpath - the XPath expression used | 使用的 XPath 表达式

Features | 主要功能:

  • Container for XPath query results - XPath 查询结果的容器
  • Access results as string, number, boolean, or element list - 以字符串、数字、布尔值或元素列表访问结果
  • Immutable record type - 不可变记录类型

Usage Examples | 使用示例:

// Access XPath result
XPathResult result = OpenXPath.evaluate(doc, "//user/name");
String name = result.asString();
List<XmlElement> elements = result.elements();

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (value can be null) - 空值安全: 是(值可以为 null)

public record XPathResult(Object value, List<XmlElement> elements, String xpath) extends Record
XPath Result - Container for XPath query results XPath 结果 - XPath 查询结果的容器

This record holds the result of an XPath query with convenience methods for accessing the result in different forms.

此记录保存 XPath 查询的结果,并提供便捷方法以不同形式访问结果。

Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • XPathResult

      public XPathResult(Object value, List<XmlElement> elements, String xpath)
      Creates an instance of a XPathResult record class.
      Parameters:
      value - the value for the value record component
      elements - the value for the elements record component
      xpath - the value for the xpath record component
  • Method Details

    • of

      public static XPathResult of(Object value, String xpath)
      Creates an XPath result from a single value. 从单个值创建 XPath 结果。
      Parameters:
      value - the value | 值
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result | 结果
    • of

      public static XPathResult of(List<XmlElement> elements, String xpath)
      Creates an XPath result from elements. 从元素创建 XPath 结果。
      Parameters:
      elements - the elements | 元素列表
      xpath - the XPath expression | XPath 表达式
      Returns:
      the result | 结果
    • asString

      public String asString()
      Returns the result as a string. 返回结果为字符串。
      Returns:
      the string value | 字符串值
    • asOptionalString

      public Optional<String> asOptionalString()
      Returns the result as an optional string. 返回结果为可选字符串。
      Returns:
      the optional string | 可选字符串
    • asInt

      public Integer asInt()
      Returns the result as an integer. 返回结果为整数。
      Returns:
      the integer value | 整数值
    • asOptionalInt

      public Optional<Integer> asOptionalInt()
      Returns the result as an optional integer. 返回结果为可选整数。
      Returns:
      the optional integer | 可选整数
    • asLong

      public Long asLong()
      Returns the result as a long. 返回结果为长整数。
      Returns:
      the long value | 长整数值
    • asDouble

      public Double asDouble()
      Returns the result as a double. 返回结果为双精度浮点数。
      Returns:
      the double value | 双精度浮点数值
    • asBoolean

      public Boolean asBoolean()
      Returns the result as a boolean. 返回结果为布尔值。
      Returns:
      the boolean value | 布尔值
    • asElement

      public Optional<XmlElement> asElement()
      Returns the first element if available. 返回第一个元素(如果可用)。
      Returns:
      the first element | 第一个元素
    • asElements

      public List<XmlElement> asElements()
      Returns all elements. 返回所有元素。
      Returns:
      the elements | 元素列表
    • isEmpty

      public boolean isEmpty()
      Returns whether the result is empty. 返回结果是否为空。
      Returns:
      true if empty | 如果为空则返回 true
    • count

      public int count()
      Returns the count of matched elements. 返回匹配元素的数量。
      Returns:
      the count | 数量
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public Object value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component
    • elements

      public List<XmlElement> elements()
      Returns the value of the elements record component.
      Returns:
      the value of the elements record component
    • xpath

      public String xpath()
      Returns the value of the xpath record component.
      Returns:
      the value of the xpath record component