可以使用 JAXB 2.0 验证编组的 XML 吗? [英] Can one validate marshalled XML with JAXB 2.0?

查看:28
本文介绍了可以使用 JAXB 2.0 验证编组的 XML 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然在 JAXB 的第 2 版中 - 验证器类已被弃用 - 这是否意味着编组器正在自动验证您的 XML?如果是这样,它似乎不会抱怨我正在形成的一些不正确的 XML!谁能给我一些关于如何验证编组 XML 以确保它符合 XSD 架构的建议.

Apparently in version 2 of JAXB - the validator class has been deprecated - does this mean the marshaller is automatically validating your XML? If so it doesn't seem to be complaining about some of the incorrect XML I am forming! Can anyone give me some advice on how I can validate marshalled XML to make sure it conforms to the XSD schema.

非常感谢.

推荐答案

通过使用 JAXP 1.3 Schema Validation Framework,JAXB 2.0 中的验证功能得到了扩展.

Validation capabilities have been expanded in JAXB 2.0 through the use of the JAXP 1.3 Schema Validation Framework.

你之前做过的地方:

unmarshaller.setValidating(true);

现在你需要做的是:

SchemaFactory sf = SchemaFactory.newInstance(
    javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("myschema.xsd"));
unmarshaller.setSchema(schema);

如果您将 null 传递给 setSchema,则会禁用验证.

If you pass null into setSchema, it disables validation.

请查看此参考资料.

这篇关于可以使用 JAXB 2.0 验证编组的 XML 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆