在我的程序中,我正在生成一个 XML 文件,并且需要针对位于不同 URL 上的多个模式来验证该 XML 文件。它只需要一次针对所有模式验证该 XML 文件。
源片段:
// here source array contain the locations of schemas, located at diff servers.
Source[] source = {
new StreamSource(
new URL("http://localhost:8081/test1/testSchema1.xsd")
.openStream()),
new StreamSource(
new URL(
"http://ccr.internal.ericsson.com/test2/testSchema2.xsd")
.openStream()) };
try {
// Compile the schema, which loaded from schemaFileLocation.
Schema schemaGrammar = schemaFactory.newSchema(source);
// Create a validator for schema.
Validator configValidator = schemaGrammar.newValidator();
// set error handler with validator.
SchemaValidator schemaValidator = new SchemaValidator();
configValidator
.setErrorHandler(schemaValidator.new MessageHandler());
// validate xml instance against the grammar.
configValidator.validate(new StreamSource(new File("xmlFile.xml")));
System.out.println("\n");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
当我运行上面的代码片段时,我得到了这个错误:
org.xml.sax.SAXParseException:src-resolve:无法将名称“xn:SubNetwork”解析为(n)“元素声明”组件。在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org .apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source) at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source) at org.apache.xerces.impl.xs.traversers .XSDElementTraverser.traverseLocal(Unknown Source) at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source) at org.apache.xerces.impl.xs.traversers.XSDAbstractParticleTraverser。
代码中的问题是什么?他们是否有任何其他方式来验证多个模式?