在 Java 中:根据位于不同服务器位置的多个模式验证 XML
在我的程序中,我正在生成一个 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' 到 a(n) ' 元素 声明'组件。在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知 来源)于 org.apache.xerces.util.ErrorHandlerWrapper.error(未知 来源)于 org.apache.xerces.impl.XMLErrorReporter.reportError(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDAbstractParticleTraverser.traverseSeqChoice(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDAbstractParticleTraverser.traverseChoice(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseLocal(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(未知 来源)于 org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(未知 来源)于 org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(未知 来源)于 org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(未知 来源)于 org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(未知 来源)于 org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(未知 来源)于 com.work.test.generateErrorLog(SchemaValidator.java:197)
代码中有什么问题?他们是否有其他方法来验证多个模式?
In my program, I am generating a XML file, and need to validate that XML file against multiple schemas which are located on different URLs. Its required to validate that XML file against all schema in onetime only.
Source fragment:
// 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();
}
When I run the above code fragment, I am get this error:
org.xml.sax.SAXParseException:
src-resolve: Cannot resolve the name
'xn:SubNetwork' to a(n) 'element
declaration' component. at
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.traverseSeqChoice(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDAbstractParticleTraverser.traverseChoice(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseLocal(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(Unknown
Source) at
org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown
Source) at
org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown
Source) at
org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown
Source) at
org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown
Source) at
org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown
Source) at
com.work.test.generateErrorLog(SchemaValidator.java:197)
What is the problem in code? Is their is any other way to validate against multiple schema?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的问题。
我改变了模式的顺序。
首先我导入了所有子架构,最后我设置了主架构。
I had the same problem.
I switched the order of the schemas.
First i imported all child schemas and at the end i set the main schema.
关于这个主题的帖子很多,我也找不到令人满意的解决方案。这就是它对我的作用。为 SchemaFactory 提供自定义资源解析器
并仅解析顶级模式。然后解析器可以(希望)解析嵌入的引用
这是 LSInputImpl
There are quite a few posts regarding this topic and I could not find a satisfying solution either. Here is how it worked for me. Give the SchemaFactory a custom resource resolver
and parse only the top level schema. The resolver can then (hopefully) resolve the embedded references
Here is the LSInputImpl