Class SchemaValidator

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

public final class SchemaValidator extends Object
Schema Validator - XML Schema (XSD) validation Schema 验证器 - XML Schema (XSD) 验证

This class provides XML validation against XSD schemas.

此类提供针对 XSD 模式的 XML 验证。

Usage Examples | 使用示例:

// Validate XML against schema
ValidationResult result = SchemaValidator.of(schemaPath)
    .validate(xml);

if (!result.isValid()) {
    System.err.println(result.getErrorSummary());
}

// Validate and throw on error
SchemaValidator.of(schemaPath)
    .validateOrThrow(xml);

Features | 主要功能:

  • XML Schema (XSD) validation - XML Schema(XSD)验证
  • Validate from string, file, or input stream - 从字符串、文件或输入流验证
  • Detailed error and warning collection - 详细的错误和警告收集
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static SchemaValidator of(Path schemaPath)
      Creates a validator from a schema file. 从模式文件创建验证器。
      Parameters:
      schemaPath - the schema file path | 模式文件路径
      Returns:
      a new validator | 新验证器
    • of

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

      public static SchemaValidator of(InputStream input)
      Creates a validator from a schema input stream. 从模式输入流创建验证器。
      Parameters:
      input - the schema input stream | 模式输入流
      Returns:
      a new validator | 新验证器
    • of

      public static SchemaValidator of(Source... sources)
      Creates a validator from multiple schema sources. 从多个模式源创建验证器。
      Parameters:
      sources - the schema sources | 模式源
      Returns:
      a new validator | 新验证器
    • validate

      public ValidationResult validate(String xml)
      Validates an XML string. 验证 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(XmlDocument document)
      Validates an XmlDocument. 验证 XmlDocument。
      Parameters:
      document - the XML document | XML 文档
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(Path path)
      Validates an XML file. 验证 XML 文件。
      Parameters:
      path - the XML file path | XML 文件路径
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(InputStream input)
      Validates an XML input stream. 验证 XML 输入流。
      Parameters:
      input - the XML input stream | XML 输入流
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(Source source)
      Validates from a Source. 从 Source 验证。
      Parameters:
      source - the XML source | XML 源
      Returns:
      the validation result | 验证结果
    • validateOrThrow

      public void validateOrThrow(String xml)
      Validates and throws if invalid. 验证,如果无效则抛出异常。
      Parameters:
      xml - the XML string | XML 字符串
      Throws:
      XmlValidationException - if validation fails | 如果验证失败则抛出
    • validateOrThrow

      public void validateOrThrow(XmlDocument document)
      Validates and throws if invalid. 验证,如果无效则抛出异常。
      Parameters:
      document - the XML document | XML 文档
      Throws:
      XmlValidationException - if validation fails | 如果验证失败则抛出
    • validateOrThrow

      public void validateOrThrow(Path path)
      Validates and throws if invalid. 验证,如果无效则抛出异常。
      Parameters:
      path - the XML file path | XML 文件路径
      Throws:
      XmlValidationException - if validation fails | 如果验证失败则抛出
    • isValid

      public boolean isValid(String xml)
      Checks if an XML string is valid. 检查 XML 字符串是否有效。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      true if valid | 如果有效则返回 true
    • isValid

      public boolean isValid(XmlDocument document)
      Checks if an XmlDocument is valid. 检查 XmlDocument 是否有效。
      Parameters:
      document - the XML document | XML 文档
      Returns:
      true if valid | 如果有效则返回 true
    • getSchema

      public Schema getSchema()
      Gets the underlying Schema. 获取底层 Schema。
      Returns:
      the Schema | Schema 对象