XML 验证:我做得对吗?

发布于 2024-07-14 06:50:20 字数 1063 浏览 4 评论 0原文

我只是想知道是否有人可以重新检查一下我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2024-07-21 06:50:20

确保 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文