Class XmlValidator

java.lang.Object
cloud.opencode.base.xml.validate.XmlValidator

public final class XmlValidator extends Object
XML Validator - XML well-formedness and validation utilities XML 验证器 - XML 格式良好性和验证工具

This class provides static utilities for XML validation.

此类提供 XML 验证的静态工具。

Usage Examples | 使用示例:

// Check if XML is well-formed
boolean isWellFormed = XmlValidator.isWellFormed(xml);

// Validate well-formedness and get errors
ValidationResult result = XmlValidator.validateWellFormedness(xml);

// Validate against XSD schema
ValidationResult result = XmlValidator.validateSchema(xml, schemaPath);

// Validate against DTD
ValidationResult result = XmlValidator.validateDtd(xml, dtdPath);

Features | 主要功能:

  • XML well-formedness validation - XML 格式良好性验证
  • Schema and DTD validation delegation - Schema 和 DTD 验证委托
  • Static utility methods for quick validation - 用于快速验证的静态工具方法

Security | 安全性:

  • Thread-safe: Yes (stateless utility, secure parsing) - 线程安全: 是(无状态工具,安全解析)
  • Null-safe: No (throws on null XML) - 空值安全: 否(null XML 抛异常)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isWellFormed

      public static boolean isWellFormed(String xml)
      Checks if an XML string is well-formed. 检查 XML 字符串是否格式良好。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      true if well-formed | 如果格式良好则返回 true
    • isWellFormed

      public static boolean isWellFormed(Path path)
      Checks if an XML file is well-formed. 检查 XML 文件是否格式良好。
      Parameters:
      path - the XML file path | XML 文件路径
      Returns:
      true if well-formed | 如果格式良好则返回 true
    • validateWellFormedness

      public static ValidationResult validateWellFormedness(String xml)
      Validates XML well-formedness. 验证 XML 格式良好性。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the validation result | 验证结果
    • validateWellFormedness

      public static ValidationResult validateWellFormedness(Path path)
      Validates XML file well-formedness. 验证 XML 文件格式良好性。
      Parameters:
      path - the XML file path | XML 文件路径
      Returns:
      the validation result | 验证结果
    • requireWellFormed

      public static void requireWellFormed(String xml)
      Validates well-formedness and throws if invalid. 验证格式良好性,如果无效则抛出异常。
      Parameters:
      xml - the XML string | XML 字符串
      Throws:
      XmlValidationException - if not well-formed | 如果格式不良好则抛出
    • requireWellFormed

      public static void requireWellFormed(Path path)
      Validates file well-formedness and throws if invalid. 验证文件格式良好性,如果无效则抛出异常。
      Parameters:
      path - the XML file path | XML 文件路径
      Throws:
      XmlValidationException - if not well-formed | 如果格式不良好则抛出
    • validateSchema

      public static ValidationResult validateSchema(String xml, Path schemaPath)
      Validates XML against an XSD schema. 针对 XSD 模式验证 XML。
      Parameters:
      xml - the XML string | XML 字符串
      schemaPath - the schema file path | 模式文件路径
      Returns:
      the validation result | 验证结果
    • validateSchema

      public static ValidationResult validateSchema(String xml, String schemaXml)
      Validates XML against an XSD schema string. 针对 XSD 模式字符串验证 XML。
      Parameters:
      xml - the XML string | XML 字符串
      schemaXml - the XSD schema string | XSD 模式字符串
      Returns:
      the validation result | 验证结果
    • validateSchema

      public static ValidationResult validateSchema(Path xmlPath, Path schemaPath)
      Validates XML file against an XSD schema. 针对 XSD 模式验证 XML 文件。
      Parameters:
      xmlPath - the XML file path | XML 文件路径
      schemaPath - the schema file path | 模式文件路径
      Returns:
      the validation result | 验证结果
    • validateSchema

      public static ValidationResult validateSchema(XmlDocument document, Path schemaPath)
      Validates XmlDocument against an XSD schema. 针对 XSD 模式验证 XmlDocument。
      Parameters:
      document - the XML document | XML 文档
      schemaPath - the schema file path | 模式文件路径
      Returns:
      the validation result | 验证结果
    • isValidAgainstSchema

      public static boolean isValidAgainstSchema(String xml, Path schemaPath)
      Checks if XML is valid against a schema. 检查 XML 是否对模式有效。
      Parameters:
      xml - the XML string | XML 字符串
      schemaPath - the schema file path | 模式文件路径
      Returns:
      true if valid | 如果有效则返回 true
    • validateDtd

      public static ValidationResult validateDtd(String xml)
      Validates XML with embedded DTD. 验证带有嵌入式 DTD 的 XML。
      Parameters:
      xml - the XML string with DTD | 带 DTD 的 XML 字符串
      Returns:
      the validation result | 验证结果
    • validateDtd

      public static ValidationResult validateDtd(String xml, Path dtdPath)
      Validates XML against an external DTD. 针对外部 DTD 验证 XML。
      Parameters:
      xml - the XML string | XML 字符串
      dtdPath - the DTD file path | DTD 文件路径
      Returns:
      the validation result | 验证结果
    • validateDtd

      public static ValidationResult validateDtd(Path xmlPath, Path dtdPath)
      Validates XML file against an external DTD. 针对外部 DTD 验证 XML 文件。
      Parameters:
      xmlPath - the XML file path | XML 文件路径
      dtdPath - the DTD file path | DTD 文件路径
      Returns:
      the validation result | 验证结果
    • schemaValidator

      public static SchemaValidator schemaValidator(Path schemaPath)
      Creates a SchemaValidator. 创建 SchemaValidator。
      Parameters:
      schemaPath - the schema file path | 模式文件路径
      Returns:
      a new SchemaValidator | 新的 SchemaValidator
    • schemaValidator

      public static SchemaValidator schemaValidator(String schemaXml)
      Creates a SchemaValidator from schema string. 从模式字符串创建 SchemaValidator。
      Parameters:
      schemaXml - the XSD schema string | XSD 模式字符串
      Returns:
      a new SchemaValidator | 新的 SchemaValidator
    • dtdValidator

      public static DtdValidator dtdValidator()
      Creates a DtdValidator. 创建 DtdValidator。
      Returns:
      a new DtdValidator | 新的 DtdValidator