Class DomParser
java.lang.Object
cloud.opencode.base.xml.dom.DomParser
DOM Parser - Parses XML to DOM Document
DOM 解析器 - 将 XML 解析为 DOM Document
This class provides methods for parsing XML content into DOM Document objects. All parsing is done securely with XXE protection enabled by default.
此类提供将 XML 内容解析为 DOM Document 对象的方法。所有解析默认启用 XXE 防护安全进行。
Features | 主要功能:
- Parse from String, File, InputStream - 从字符串、文件、输入流解析
- Secure parsing (XXE protection) - 安全解析(XXE 防护)
- Namespace-aware parsing - 支持命名空间的解析
Usage Examples | 使用示例:
// Parse XML string
Document doc = DomParser.parse("<root><item>value</item></root>");
// Parse from file
Document doc = DomParser.parse(Path.of("config.xml"));
// Parse with namespace awareness
Document doc = DomParser.parseNamespaceAware(xmlString);
Security | 安全性:
- Thread-safe: Yes (stateless utility, secure parsing) - 线程安全: 是(无状态工具,安全解析)
- Null-safe: No (throws on null input) - 空值安全: 否(null 输入抛异常)
Performance | 性能特性:
- Time complexity: O(n) where n=input size in characters/bytes - 时间复杂度: O(n),n 为输入的字符/字节数
- Space complexity: O(n) as the entire DOM tree is held in memory - 空间复杂度: O(n),整个 DOM 树保存在内存中
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentCreates a new empty Document.static DocumentcreateDocument(String rootElementName) Creates a new Document with a root element.static booleanisValidXml(String xml) Validates whether the given string is well-formed XML.static Documentparse(InputStream input) Parses XML from input stream to Document.static DocumentParses XML string to Document.static DocumentParses XML file to Document.static DocumentparseNamespaceAware(InputStream input) Parses XML from input stream with namespace awareness.static DocumentParses XML string with namespace awareness.static DocumentparseNamespaceAware(Path path) Parses XML file with namespace awareness.
-
Method Details
-
parse
Parses XML string to Document. 解析 XML 字符串为 Document。- Parameters:
xml- the XML string | XML 字符串- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
parse
Parses XML file to Document. 解析 XML 文件为 Document。- Parameters:
path- the file path | 文件路径- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
parse
Parses XML from input stream to Document. 从输入流解析 XML 为 Document。- Parameters:
input- the input stream | 输入流- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
parseNamespaceAware
Parses XML string with namespace awareness. 解析 XML 字符串(支持命名空间)。- Parameters:
xml- the XML string | XML 字符串- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
parseNamespaceAware
Parses XML file with namespace awareness. 解析 XML 文件(支持命名空间)。- Parameters:
path- the file path | 文件路径- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
parseNamespaceAware
Parses XML from input stream with namespace awareness. 从输入流解析 XML(支持命名空间)。- Parameters:
input- the input stream | 输入流- Returns:
- the Document | Document
- Throws:
XmlParseException- if parsing fails | 如果解析失败则抛出异常
-
createDocument
Creates a new empty Document. 创建新的空 Document。- Returns:
- a new empty Document | 新的空 Document
-
createDocument
-
isValidXml
Validates whether the given string is well-formed XML. 验证给定字符串是否是格式正确的 XML。- Parameters:
xml- the XML string | XML 字符串- Returns:
- true if valid XML | 如果是有效 XML 则返回 true
-