我知道如何使用:
String xsdPathname = getClass.getResource("/META-INF/xsd/my.xsd").getFile();
Schema schema = sf.newSchema(new File(pathname));
Validator validator = schema.newValidator();
validator.validate(source);
但据我所知,当 XSD 包装在罐子里时,这是不可用的。
是否有其他"模式"构建方法可以接受"输入流"而不是"文件"?
您可以使用 SchemaFactory#newSchema(Source(,提供 StreamSource。因此,您的代码看起来像
InputStream xsdInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/META-INF/xsd/my.xsd");
Schema schema = sf.newSchema(new StreamSource(xsdInputStream));
Validator validator = schema.newValidator();
validator.validate(source);