在运行时使用加载模式validationXML,失败取决于模式顺序

我正在尝试进行xmlvalidation。 我在运行时获得了一个模式列表(可能包含在jar中)。 validation根据我向SchemaFactory提供模式的顺序传递或失败。

这是我在做的事情:

private void validateXml(String xml, List schemas){ Source[] source = new StreamSource[schemas.size()]; int i=0; for (URI f : schemas){ source[i++] = new StreamSource(f.openStream()); } SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NA_URI); sf.setResourceResolver(new MyClassPathResourceResolver()); Schema schema = schemaFactory.newSchema(source); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())); 

再次,如果传递的模式集不以xml的根元素引用的模式开始,则会失败。 有没有解决这个问题或我做错了什么?

默认情况下,如果模式文档已具有相同名称空间的模式文档,则Xerces将忽略模式文档。 可以使用factory选项更改此行为

http://apache.org/xml/features/validation/schema/handle-multiple-imports

首先,您必须通过调用registerErrorHandler()方法在XML阅读器上设置org.xml.sax.ErrorHandler对象的实例。 您可能会收到警告,告诉您有关问题的线索。

其次,您必须知道您正在使用哪个xml库。 在代码中调用schemaFactory.getClass()。getName()并打印它。 了解库后,如果它支持打开/关闭多个模式导入的function,则可以参考其文档。