JAXB - Validate Document before It is Unmarshalled

Validation

A considerable part of the XML Schema language deals with facets, enabling the programmer to restrict the basic datatypes. We have seen that the JAXB compiler doesn't care much about these facets as it just translates the basic datatype into one of Java's built-in types. A meticulous interpretation of these facets for checking that the XML data meets the constraints must be done during a schema validation.

If you want to validate your document before it is unmarshalled, JAXB lets you request validation by passing an object of the class javax.xml.validation.Schema to the Unmarshaller object. First, you create this schema object by setting up a schema factory for the schema language of your choice. Then you create the Schema object by calling the factory's method newSchema:

Schema mySchema;
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
try {
    mySchema = sf.newSchema( file );
} catch( SAXException saxe ){
    // ...(error handling)
    mySchema = null;
}

After the Unmarshaller object has been established, you pass it the schema.

JAXBContext jc = JAXBContext.newInstance( packagePath );
Unmarshaller u = jc.createUnmarshaller();
u.setSchema( mySchema );

Basically that's all there is to it. If the XML data validation fails, an UnmarshalException (from javax.xml.bind) is thrown. Make sure to let the user of your program see the exception message so that the problem can be fixed. If you'd like to create your own error messages, you can pass a ValidationEventCollector to the unmarshaller which will store validation events into it so that you can retrieve an event and query its individual attributes. Insert these lines before you call the unmarshal method:

ValidationEventCollector vec = new ValidationEventCollector();
u.setEventHandler( vec );

The best place for checking the event collector is in the finally phrase of the try statement wrapping all of this:

if( vec != null && vec.hasEvents() ){
    for( ValidationEvent ve: vec.getEvents() ){
        String msg = ve.getMessage();
        ValidationEventLocator vel = ve.getLocator();
        int line = vel.getLineNumber();
        int column = vel.getColumnNumber();
        System.err.println( origin + ": " + line + "." + column + ": " + msg ); 
    }
}

Now this looks as if the validation process would be kind enough to present you with all the errors in your XML document, or at least as many as possible but, alas, it appears that the validation process throws an exception as soon as the first deviation is detected. If you want to continue as long as possible, you'll have to catch all errors with a ValidationEventHandler.

 

Validation Event Handling

The interface javax.xml.bind.ValidationEventHandler is quite simple. Implementing classes must provide a single method to catch a ValidationEvent as we've seen it in the previous section.

boolean handleEvent( ValidationEvent event )

To register, the Unmarshaller method setEventHandler is called. If the calling object is implementing the event handler interface, we might write:

Unmarshaller u = jc.createUnmarshaller();
u.setEventHandler( this );

 

转载于:https://www.cnblogs.com/huey/p/5506615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值