Class SaxParser
java.lang.Object
cloud.opencode.base.xml.sax.SaxParser
SAX Parser - Event-driven streaming XML parser
SAX 解析器 - 事件驱动的流式 XML 解析器
This class provides SAX parsing with a simplified, fluent API. SAX parsing is memory-efficient for large XML files.
此类提供简化流式 API 的 SAX 解析。SAX 解析对于大型 XML 文件内存效率高。
Usage Examples | 使用示例:
// Using SimpleSaxHandler
List<User> users = new ArrayList<>();
SaxParser.createSecure()
.handler(SimpleSaxHandler.create()
.onStart("user", (name, attrs) -> {
User user = new User();
user.setId(Long.parseLong(attrs.get("id")));
users.add(user);
})
.onText("name", text -> users.getLast().setName(text))
.onText("email", text -> users.getLast().setEmail(text))
)
.parse(xml);
Features | 主要功能:
- Event-driven streaming XML parsing - 事件驱动的流式 XML 解析
- Memory-efficient for large XML files - 大型 XML 文件的内存高效处理
- Secure parsing with XXE protection - 带 XXE 防护的安全解析
- Fluent builder API - 流式构建器 API
Security | 安全性:
- Thread-safe: No (not designed for shared use) - 线程安全: 否(不适用于共享使用)
- Null-safe: No (throws on null XML/handler) - 空值安全: 否(null XML/处理器抛异常)
Performance | 性能特性:
- Time complexity: O(n) where n=input size in characters/bytes - 时间复杂度: O(n),n 为输入的字符/字节数
- Space complexity: O(1) - event-driven streaming; no document tree is built in memory - 空间复杂度: O(1) - 事件驱动的流式处理;不在内存中构建文档树
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic SaxParsercreate()Creates a new SAX parser.static SaxParserCreates a secure SAX parser (XXE protection).handler(SaxHandler handler) Sets the handler.handler(SimpleSaxHandler handler) Sets a simple handler.namespaceAware(boolean enabled) Enables namespace awareness.voidparse(InputStream input) Parses from an input stream.voidParses an XML string.voidParses from a file.secure(boolean enabled) Enables secure mode (XXE protection).validating(boolean enabled) Enables validation.
-
Method Details
-
create
Creates a new SAX parser. 创建新的 SAX 解析器。- Returns:
- a new parser | 新解析器
-
createSecure
Creates a secure SAX parser (XXE protection). 创建安全的 SAX 解析器(XXE 防护)。- Returns:
- a new secure parser | 新的安全解析器
-
handler
Sets the handler. 设置处理器。- Parameters:
handler- the SAX handler | SAX 处理器- Returns:
- this parser for chaining | 此解析器以便链式调用
-
handler
Sets a simple handler. 设置简化处理器。- Parameters:
handler- the simple handler | 简化处理器- Returns:
- this parser for chaining | 此解析器以便链式调用
-
namespaceAware
Enables namespace awareness. 启用命名空间感知。- Parameters:
enabled- whether enabled | 是否启用- Returns:
- this parser for chaining | 此解析器以便链式调用
-
validating
Enables validation. 启用验证。- Parameters:
enabled- whether enabled | 是否启用- Returns:
- this parser for chaining | 此解析器以便链式调用
-
secure
Enables secure mode (XXE protection). 启用安全模式(XXE 防护)。- Parameters:
enabled- whether enabled | 是否启用- Returns:
- this parser for chaining | 此解析器以便链式调用
-
parse
Parses an XML string. 解析 XML 字符串。- Parameters:
xml- the XML string | XML 字符串
-
parse
Parses from an input stream. 从输入流解析。- Parameters:
input- the input stream | 输入流
-
parse
-