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

查看:118
本文介绍了可以使用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模式验证框架,在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天全站免登陆