使用针对多个 XML 模式验证的 Moxy 解组 xml 文件

Unmarshal xml file with Moxy validating against multiple XML schemas

我正在尝试使用 EclipseLink MOXy 2.6 针对多个 XML 模式解组一个 XML 文件 - common.xsduserOfCommon.xsd.
userOfCommon.xsd 包括 common.xsd 并使用其中定义的某些类型。

如果你想要验证,你必须像这样设置解组器:

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Source common = new StreamSource(this.getClass().getResourceAsStream("xsd/common.xsd"));
Source userOfCommon = new StreamSource(this.getClass().getResourceAsStream("xsd/userOfCommon.xsd"));

Schema schema = sf.newSchema(new Source[] {common, userOfCommon});
unmarshaller.setSchema(sf.newSchema(schema));

但是设置模式会报错 "Failing to resolve the 'Some ComplexType Name' to a(n) 'type definition' component."

我试过将架构设置为这样的文件名

Schema schema = sf.newSchema(new File("xsd/userOfCommon.xsd"));

并且有效。但我想将模式设置为源项目,以便从类路径加载它。

有什么实现方法的建议吗?

实际上此处给出的答案 适用于这种情况。

我必须像这样在 SchemaFactory 中设置自定义 ResourceResolver

sf.setResourceResolver(new ResourceResolver());

ResourceResolver 的代码可用here

在这种情况下,无需导入多个架构源。您可以像这样导入主模式源:

Schema schema = sf.newSchema(userOfCommon);

但是我仍然没有弄清楚如何在不使用自定义 ResourceResolver 的情况下使用多个模式源来实现它。

尝试在创建 StreamSource 时使用位置,如下所示:

Source common = new StreamSource(this.getClass().getResource("xsd/common.xsd").toString());
Source userOfCommon = new StreamSource(this.getClass().getResource("xsd/userOfCommon.xsd").toString());

该位置用于相对解析模式导入