Class StaxUtil

java.lang.Object
cloud.opencode.base.xml.stax.StaxUtil

public final class StaxUtil extends Object
StAX Utility - Utility methods for StAX operations StAX 工具类 - StAX 操作的工具方法

This class provides utility methods for common StAX operations.

此类提供常见 StAX 操作的工具方法。

Features | 主要功能:

  • Utility methods for common StAX operations - 常见 StAX 操作的工具方法
  • XML content extraction and transformation helpers - XML 内容提取和转换辅助方法

Usage Examples | 使用示例:

// Extract text from XML element
String text = StaxUtil.getElementText(reader, "name");

// Read attributes into map
Map<String, String> attrs = StaxUtil.getAttributes(reader);

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
  • Null-safe: No (throws on null reader) - 空值安全: 否(null 读取器抛异常)

Performance | 性能特性:

  • Time complexity: O(n) for forEachEvent/forEachStartElement/collectElementNames/copyElement where n=number of XML events or subtree size; O(1) for individual reader/writer creation and attribute access - 时间复杂度: forEachEvent/forEachStartElement/collectElementNames/copyElement 为 O(n),n 为 XML 事件数或子树大小;读写器创建和属性访问为 O(1)
  • Space complexity: O(1) for streaming iteration operations; O(n) for collectElementNames result list - 空间复杂度: 流式迭代操作为 O(1);collectElementNames 结果列表为 O(n)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getEventTypeName

      public static String getEventTypeName(int eventType)
      Gets the event type name. 获取事件类型名称。
      Parameters:
      eventType - the event type constant | 事件类型常量
      Returns:
      the event type name | 事件类型名称
    • createSecureInputFactory

      public static XMLInputFactory createSecureInputFactory()
      Creates a secure XMLInputFactory. 创建安全的 XMLInputFactory。
      Returns:
      a secure factory | 安全的工厂
    • createOutputFactory

      public static XMLOutputFactory createOutputFactory()
      Creates an XMLOutputFactory. 创建 XMLOutputFactory。
      Returns:
      a new factory | 新工厂
    • createReader

      public static XMLStreamReader createReader(String xml)
      Creates a secure XMLStreamReader from a string. 从字符串创建安全的 XMLStreamReader。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      a new reader | 新读取器
    • createReader

      public static XMLStreamReader createReader(InputStream input)
      Creates a secure XMLStreamReader from an input stream. 从输入流创建安全的 XMLStreamReader。
      Parameters:
      input - the input stream | 输入流
      Returns:
      a new reader | 新读取器
    • createReader

      public static XMLStreamReader createReader(Path path)
      Creates a secure XMLStreamReader from a file. 从文件创建安全的 XMLStreamReader。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a new reader | 新读取器
    • createWriter

      public static XMLStreamWriter createWriter(StringWriter writer)
      Creates an XMLStreamWriter to a StringWriter. 创建写入 StringWriter 的 XMLStreamWriter。
      Parameters:
      writer - the string writer | 字符串写入器
      Returns:
      a new writer | 新写入器
    • createWriter

      public static XMLStreamWriter createWriter(OutputStream output)
      Creates an XMLStreamWriter to an output stream. 创建写入输出流的 XMLStreamWriter。
      Parameters:
      output - the output stream | 输出流
      Returns:
      a new writer | 新写入器
    • createWriter

      public static XMLStreamWriter createWriter(Path path)
      Creates an XMLStreamWriter to a file. 创建写入文件的 XMLStreamWriter。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a new writer | 新写入器
    • forEachEvent

      public static void forEachEvent(String xml, Consumer<XMLStreamReader> consumer)
      Iterates over all events in XML. 迭代 XML 中的所有事件。
      Parameters:
      xml - the XML string | XML 字符串
      consumer - the event consumer | 事件消费者
    • forEachStartElement

      public static void forEachStartElement(String xml, Consumer<XMLStreamReader> consumer)
      Iterates over start elements in XML. 迭代 XML 中的开始元素。
      Parameters:
      xml - the XML string | XML 字符串
      consumer - the element consumer | 元素消费者
    • collectElementNames

      public static List<String> collectElementNames(String xml)
      Collects all element names from XML. 从 XML 收集所有元素名称。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      list of element names | 元素名称列表
    • countElements

      public static int countElements(String xml, String elementName)
      Counts elements with a specific name. 计算具有特定名称的元素数量。
      Parameters:
      xml - the XML string | XML 字符串
      elementName - the element name | 元素名称
      Returns:
      the count | 计数
    • getAttributes

      public static Map<String,String> getAttributes(XMLStreamReader reader)
      Gets attributes from a reader as a map. 从读取器获取属性作为映射。
      Parameters:
      reader - the reader | 读取器
      Returns:
      map of attributes | 属性映射
    • getAttribute

      public static Optional<String> getAttribute(XMLStreamReader reader, String localName)
      Gets an optional attribute value. 获取可选的属性值。
      Parameters:
      reader - the reader | 读取器
      localName - the attribute name | 属性名称
      Returns:
      Optional containing the value | 包含值的 Optional
    • getElementTextSafe

      public static String getElementTextSafe(XMLStreamReader reader)
      Gets element text safely. 安全获取元素文本。
      Parameters:
      reader - the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)
      Returns:
      the element text | 元素文本
    • skipTo

      public static boolean skipTo(XMLStreamReader reader, String elementName)
      Skips to an element with the given name. 跳转到具有给定名称的元素。
      Parameters:
      reader - the reader | 读取器
      elementName - the element name | 元素名称
      Returns:
      true if found | 如果找到则返回 true
    • copyElement

      public static void copyElement(XMLStreamReader reader, XMLStreamWriter writer)
      Copies an element and its content to a writer. 将元素及其内容复制到写入器。
      Parameters:
      reader - the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)
      writer - the writer | 写入器
    • elementToXml

      public static String elementToXml(XMLStreamReader reader)
      Converts an element to XML string. 将元素转换为 XML 字符串。
      Parameters:
      reader - the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)
      Returns:
      the XML string | XML 字符串
    • closeQuietly

      public static void closeQuietly(XMLStreamReader reader)
      Closes a reader quietly. 安静地关闭读取器。
      Parameters:
      reader - the reader | 读取器
    • closeQuietly

      public static void closeQuietly(XMLStreamWriter writer)
      Closes a writer quietly. 安静地关闭写入器。
      Parameters:
      writer - the writer | 写入器