Class StaxUtil
java.lang.Object
cloud.opencode.base.xml.stax.StaxUtil
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 Summary
Modifier and TypeMethodDescriptionstatic voidcloseQuietly(XMLStreamReader reader) Closes a reader quietly.static voidcloseQuietly(XMLStreamWriter writer) Closes a writer quietly.Collects all element names from XML.static voidcopyElement(XMLStreamReader reader, XMLStreamWriter writer) Copies an element and its content to a writer.static intcountElements(String xml, String elementName) Counts elements with a specific name.static XMLOutputFactoryCreates an XMLOutputFactory.static XMLStreamReadercreateReader(InputStream input) Creates a secure XMLStreamReader from an input stream.static XMLStreamReadercreateReader(String xml) Creates a secure XMLStreamReader from a string.static XMLStreamReadercreateReader(Path path) Creates a secure XMLStreamReader from a file.static XMLInputFactoryCreates a secure XMLInputFactory.static XMLStreamWritercreateWriter(OutputStream output) Creates an XMLStreamWriter to an output stream.static XMLStreamWritercreateWriter(StringWriter writer) Creates an XMLStreamWriter to a StringWriter.static XMLStreamWritercreateWriter(Path path) Creates an XMLStreamWriter to a file.static StringelementToXml(XMLStreamReader reader) Converts an element to XML string.static voidforEachEvent(String xml, Consumer<XMLStreamReader> consumer) Iterates over all events in XML.static voidforEachStartElement(String xml, Consumer<XMLStreamReader> consumer) Iterates over start elements in XML.getAttribute(XMLStreamReader reader, String localName) Gets an optional attribute value.getAttributes(XMLStreamReader reader) Gets attributes from a reader as a map.static StringgetElementTextSafe(XMLStreamReader reader) Gets element text safely.static StringgetEventTypeName(int eventType) Gets the event type name.static booleanskipTo(XMLStreamReader reader, String elementName) Skips to an element with the given name.
-
Method Details
-
getEventTypeName
Gets the event type name. 获取事件类型名称。- Parameters:
eventType- the event type constant | 事件类型常量- Returns:
- the event type name | 事件类型名称
-
createSecureInputFactory
Creates a secure XMLInputFactory. 创建安全的 XMLInputFactory。- Returns:
- a secure factory | 安全的工厂
-
createOutputFactory
Creates an XMLOutputFactory. 创建 XMLOutputFactory。- Returns:
- a new factory | 新工厂
-
createReader
Creates a secure XMLStreamReader from a string. 从字符串创建安全的 XMLStreamReader。- Parameters:
xml- the XML string | XML 字符串- Returns:
- a new reader | 新读取器
-
createReader
Creates a secure XMLStreamReader from an input stream. 从输入流创建安全的 XMLStreamReader。- Parameters:
input- the input stream | 输入流- Returns:
- a new reader | 新读取器
-
createReader
Creates a secure XMLStreamReader from a file. 从文件创建安全的 XMLStreamReader。- Parameters:
path- the file path | 文件路径- Returns:
- a new reader | 新读取器
-
createWriter
Creates an XMLStreamWriter to a StringWriter. 创建写入 StringWriter 的 XMLStreamWriter。- Parameters:
writer- the string writer | 字符串写入器- Returns:
- a new writer | 新写入器
-
createWriter
Creates an XMLStreamWriter to an output stream. 创建写入输出流的 XMLStreamWriter。- Parameters:
output- the output stream | 输出流- Returns:
- a new writer | 新写入器
-
createWriter
Creates an XMLStreamWriter to a file. 创建写入文件的 XMLStreamWriter。- Parameters:
path- the file path | 文件路径- Returns:
- a new writer | 新写入器
-
forEachEvent
Iterates over all events in XML. 迭代 XML 中的所有事件。- Parameters:
xml- the XML string | XML 字符串consumer- the event consumer | 事件消费者
-
forEachStartElement
Iterates over start elements in XML. 迭代 XML 中的开始元素。- Parameters:
xml- the XML string | XML 字符串consumer- the element consumer | 元素消费者
-
collectElementNames
-
countElements
-
getAttributes
Gets attributes from a reader as a map. 从读取器获取属性作为映射。- Parameters:
reader- the reader | 读取器- Returns:
- map of attributes | 属性映射
-
getAttribute
Gets an optional attribute value. 获取可选的属性值。- Parameters:
reader- the reader | 读取器localName- the attribute name | 属性名称- Returns:
- Optional containing the value | 包含值的 Optional
-
getElementTextSafe
Gets element text safely. 安全获取元素文本。- Parameters:
reader- the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)- Returns:
- the element text | 元素文本
-
skipTo
Skips to an element with the given name. 跳转到具有给定名称的元素。- Parameters:
reader- the reader | 读取器elementName- the element name | 元素名称- Returns:
- true if found | 如果找到则返回 true
-
copyElement
Copies an element and its content to a writer. 将元素及其内容复制到写入器。- Parameters:
reader- the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)writer- the writer | 写入器
-
elementToXml
Converts an element to XML string. 将元素转换为 XML 字符串。- Parameters:
reader- the reader (must be at START_ELEMENT) | 读取器(必须在 START_ELEMENT)- Returns:
- the XML string | XML 字符串
-
closeQuietly
Closes a reader quietly. 安静地关闭读取器。- Parameters:
reader- the reader | 读取器
-
closeQuietly
Closes a writer quietly. 安静地关闭写入器。- Parameters:
writer- the writer | 写入器
-