XML 验证:我做得对吗?
我只是想知道是否有人可以重新检查一下我的 XML 验证代码,看看我做得是否正确。 这是给我带来麻烦的代码部分...
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// *** CODE FAILS ON THE BELOW LINE **/
factory.setSchema(schemaFactory
.newSchema(new Source[] { new StreamSource(schemaStream) }));
SAXParser parser = factory.newSAXParser();
SAXReader reader = new SAXReader(parser.getXMLReader());
reader.setValidation(false);
reader.setErrorHandler(new ResultProducingErrorHandler());
reader.read(content);
每当我运行上面的代码时,我都会收到如下错误:
src-resolve: Cannot resolve the name 'ns:myStructure' to a(n) 'type definition' component.
错误消息中提到的元素都是通过调用 < 导入到架构中的元素;xs:导入/>。 该架构似乎通过 W3C XML 架构验证器 验证正常。
我是否必须单独包含这些模式中的每一个,或者 Java 是否足够智能来获取这些额外的模式? 我尝试将它们添加到传递给 newSchema 调用的数组中,但这没有任何区别。
我不认为我可以给出模式的链接,所以我真的只是在寻找是或否关于我的代码看起来是否至少可以接受。
I was just wondering if someone could give my XML validation code a once over to see if I'm doing it right. Here's the portion of code that is giving me the trouble...
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// *** CODE FAILS ON THE BELOW LINE **/
factory.setSchema(schemaFactory
.newSchema(new Source[] { new StreamSource(schemaStream) }));
SAXParser parser = factory.newSAXParser();
SAXReader reader = new SAXReader(parser.getXMLReader());
reader.setValidation(false);
reader.setErrorHandler(new ResultProducingErrorHandler());
reader.read(content);
Whenever I run the above code, I get an error along the lines of:
src-resolve: Cannot resolve the name 'ns:myStructure' to a(n) 'type definition' component.
The elements mentioned in the error messages are all ones that are imported into the schema via calls to <xs:import />. The schema seems to validate OK via the W3C XML Schema Validator.
Do I have to include each of these schema's individually or is Java smart enough to go off and fetch these extra schema's too? I tried adding them in the array passed to the newSchema call but that didn't make any difference.
I don't think I can give out the link to the schema, so I'm really just looking for a yes or no regarding if my code looks at least acceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保 xs:import 语句指向可从应用程序的当前目录访问的路径。 当前目录可能不是您想象的那样。
Ensure that the xs:import statements point to paths that are reachable from the current directory of your application. The current directory may not be what you think it is.