Class OpenXml
java.lang.Object
cloud.opencode.base.xml.OpenXml
OpenXml - Unified facade for XML operations
OpenXml - XML 操作的统一门面
This class provides a unified entry point for all XML operations in the opencode-base-xml module.
此类为 opencode-base-xml 模块中的所有 XML 操作提供统一入口点。
Features | 主要功能:
- XML parsing from String, File, InputStream - 从字符串、文件、输入流解析 XML
- Fluent XML document building - 流式 XML 文档构建
- XPath querying with namespace support - 支持命名空间的 XPath 查询
- XML Schema and DTD validation - XML Schema 和 DTD 验证
- XSLT transformation - XSLT 转换
- XML-to-object binding and marshalling - XML 到对象绑定和编组
- SAX and StAX streaming parsers - SAX 和 StAX 流式解析器
- Namespace management utilities - 命名空间管理工具
Usage Examples | 使用示例:
// Parse XML
XmlDocument doc = OpenXml.parse(xmlString);
XmlDocument doc = OpenXml.parseFile(path);
// Build XML
XmlDocument doc = OpenXml.builder("root")
.element("child", "value")
.build();
// XPath queries
String value = OpenXml.xpath(doc, "//name/text()");
List<XmlElement> elements = OpenXml.xpathElements(doc, "//item");
// Validate
boolean valid = OpenXml.isWellFormed(xml);
ValidationResult result = OpenXml.validateSchema(xml, schemaPath);
// Transform
String formatted = OpenXml.format(xml, 4);
String result = OpenXml.xslt(xml, xsltPath);
// Bind to objects
User user = OpenXml.unmarshal(xml, User.class);
String xml = OpenXml.marshal(user);
Security | 安全性:
- Thread-safe: Yes (all static methods, no shared mutable state) - 线程安全: 是(全部静态方法,无共享可变状态)
- Null-safe: No (null inputs throw exceptions) - 空值安全: 否(空值输入抛出异常)
- XXE protection enabled by default - 默认启用 XXE 防护
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic XmlBinderbinder()Creates an XML binder.static XmlBuilderCreates an XML builder with the given root element name.static XmlBuilderCreates an XML builder with namespace.static ElementBuilderCreates an element builder.extractNamespaces(XmlDocument document) Extracts namespaces from a document.static StringFormats XML with default indentation (4 spaces).static StringFormats XML with indentation.static booleanisWellFormed(String xml) Checks if XML is well-formed.static StringMarshals an object to XML string.static StringMarshals an object to formatted XML string.static StringMinifies XML by removing whitespace.static OpenNamespaceContextCreates a namespace context.static OpenNamespaceContextnamespaceContext(XmlDocument document) Creates a namespace context from a document.static XmlDocumentparse(InputStream input) Parses an XML input stream to XmlDocument.static XmlDocumentParses an XML string to XmlDocument.static XmlDocumentParses an XML file to XmlDocument.static SaxParserCreates a SAX parser.static SchemaValidatorschemaValidator(Path schemaPath) Creates a schema validator.static StaxReaderstaxReader(String xml) Creates a StAX reader.static StaxReaderstaxReader(Path path) Creates a StAX reader from file.static StaxWriterCreates a StAX writer.static StaxWriterstaxWriter(Path path) Creates a StAX writer to file.static <T> Tunmarshal(XmlDocument document, Class<T> clazz) Unmarshals XmlDocument to an object.static <T> TUnmarshals XML to an object.static ValidationResultvalidateSchema(String xml, Path schemaPath) Validates XML against a schema.static ValidationResultValidates XML well-formedness.static Stringxpath(XmlDocument doc, String xpath) Evaluates an XPath expression and returns a string.static StringEvaluates an XPath expression with namespaces.static Stringxpath(XmlElement element, String xpath) Evaluates an XPath expression on an element.static List<XmlElement> xpathElements(XmlDocument doc, String xpath) Evaluates an XPath expression and returns elements.static StringTransforms XML using XSLT.static XsltTransformerxsltTransformer(String xslt) Creates an XSLT transformer from string.static XsltTransformerxsltTransformer(Path xsltPath) Creates an XSLT transformer.
-
Method Details
-
parse
Parses an XML string to XmlDocument. 将 XML 字符串解析为 XmlDocument。- Parameters:
xml- the XML string | XML 字符串- Returns:
- the parsed document | 解析后的文档
-
parseFile
Parses an XML file to XmlDocument. 将 XML 文件解析为 XmlDocument。- Parameters:
path- the file path | 文件路径- Returns:
- the parsed document | 解析后的文档
-
parse
Parses an XML input stream to XmlDocument. 将 XML 输入流解析为 XmlDocument。- Parameters:
input- the input stream | 输入流- Returns:
- the parsed document | 解析后的文档
-
builder
Creates an XML builder with the given root element name. 使用给定的根元素名称创建 XML 构建器。- Parameters:
rootName- the root element name | 根元素名称- Returns:
- a new builder | 新构建器
-
builder
Creates an XML builder with namespace. 使用命名空间创建 XML 构建器。- Parameters:
namespaceUri- the namespace URI | 命名空间 URIrootName- the root element name | 根元素名称- Returns:
- a new builder | 新构建器
-
element
Creates an element builder. 创建元素构建器。- Parameters:
name- the element name | 元素名称- Returns:
- a new element builder | 新元素构建器
-
xpath
Evaluates an XPath expression and returns a string. 计算 XPath 表达式并返回字符串。- Parameters:
doc- the document | 文档xpath- the XPath expression | XPath 表达式- Returns:
- the result string | 结果字符串
-
xpath
Evaluates an XPath expression with namespaces. 使用命名空间计算 XPath 表达式。- Parameters:
doc- the document | 文档xpath- the XPath expression | XPath 表达式namespaces- the namespace map | 命名空间映射- Returns:
- the result string | 结果字符串
-
xpathElements
Evaluates an XPath expression and returns elements. 计算 XPath 表达式并返回元素列表。- Parameters:
doc- the document | 文档xpath- the XPath expression | XPath 表达式- Returns:
- list of matching elements | 匹配元素列表
-
xpath
Evaluates an XPath expression on an element. 在元素上计算 XPath 表达式。- Parameters:
element- the element | 元素xpath- the XPath expression | XPath 表达式- Returns:
- the result string | 结果字符串
-
isWellFormed
Checks if XML is well-formed. 检查 XML 是否格式良好。- Parameters:
xml- the XML string | XML 字符串- Returns:
- true if well-formed | 如果格式良好则返回 true
-
validateWellFormedness
Validates XML well-formedness. 验证 XML 格式良好性。- Parameters:
xml- the XML string | XML 字符串- Returns:
- the validation result | 验证结果
-
validateSchema
Validates XML against a schema. 针对模式验证 XML。- Parameters:
xml- the XML string | XML 字符串schemaPath- the schema path | 模式路径- Returns:
- the validation result | 验证结果
-
schemaValidator
Creates a schema validator. 创建模式验证器。- Parameters:
schemaPath- the schema path | 模式路径- Returns:
- a new validator | 新验证器
-
format
-
format
-
minify
-
xslt
-
xsltTransformer
Creates an XSLT transformer. 创建 XSLT 转换器。- Parameters:
xsltPath- the XSLT path | XSLT 路径- Returns:
- a new transformer | 新转换器
-
xsltTransformer
Creates an XSLT transformer from string. 从字符串创建 XSLT 转换器。- Parameters:
xslt- the XSLT string | XSLT 字符串- Returns:
- a new transformer | 新转换器
-
unmarshal
-
unmarshal
Unmarshals XmlDocument to an object. 将 XmlDocument 解组为对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
document- the document | 文档clazz- the target class | 目标类- Returns:
- the unmarshalled object | 解组的对象
-
marshal
-
marshal
-
binder
-
saxParser
Creates a SAX parser. 创建 SAX 解析器。- Returns:
- a new SAX parser | 新 SAX 解析器
-
staxReader
Creates a StAX reader. 创建 StAX 读取器。- Parameters:
xml- the XML string | XML 字符串- Returns:
- a new StAX reader | 新 StAX 读取器
-
staxReader
Creates a StAX reader from file. 从文件创建 StAX 读取器。- Parameters:
path- the file path | 文件路径- Returns:
- a new StAX reader | 新 StAX 读取器
-
staxWriter
Creates a StAX writer. 创建 StAX 写入器。- Returns:
- a new StAX writer | 新 StAX 写入器
-
staxWriter
Creates a StAX writer to file. 创建写入文件的 StAX 写入器。- Parameters:
path- the file path | 文件路径- Returns:
- a new StAX writer | 新 StAX 写入器
-
namespaceContext
Creates a namespace context. 创建命名空间上下文。- Returns:
- a new namespace context | 新命名空间上下文
-
namespaceContext
Creates a namespace context from a document. 从文档创建命名空间上下文。- Parameters:
document- the document | 文档- Returns:
- a namespace context | 命名空间上下文
-
extractNamespaces
Extracts namespaces from a document. 从文档提取命名空间。- Parameters:
document- the document | 文档- Returns:
- map of prefix to URI | 前缀到 URI 的映射
-