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 );
JAXB - Validate Document before It is Unmarshalled的更多相关文章
- 《JavaScript 源码分析》之 jquery.unobtrusive-ajax.js
/*! ** Unobtrusive Ajax support library for jQuery ** Copyright (C) Microsoft Corporation. All right ...
- dom4j使用总结
1.加载Xml 从文件加载 SAXReader reader = new SAXReader(); String filePath = "/xmlfile/" + fileName ...
- 利用JS提交表单的几种方法和验证
第一种方式:表单提交,在form标签中增加onsubmit事件来判断表单提交是否成功 <script type="text/javascript"> function ...
- jquery.unobtrusive-ajax.js的扩展,做到片段式加载
//ajax支持库 /*! ** Unobtrusive Ajax support library for jQuery ** Copyright (C) Microsoft Corporation. ...
- jquery.unobtrusive-ajax.js源码阅读
/*! ** Unobtrusive Ajax support library for jQuery ** Copyright (C) Microsoft Corporation. All right ...
- ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能
首先放出该插件的下载地址:http://pan.baidu.com/s/1Aa3yD,里面有帮助文档,详细了解可以自行下载学习,本章只讲解一些基本的验证功能,页面代码如下: <%@ Page L ...
- 纯JS实现图片验证码功能并兼容IE6-8
最近要搞一个图片验证码功能,但是又不想自己写后台代码.于是自己准备搞一个纯前端的验证码功能,于是网上搜索了一下,找到一个插件gVerify.js,简单好用,实现完美.不过后面接到说要兼容IE8,想想也 ...
- AugularJS从入门到实践(三)
前 言 前端 AngularJS是为了克服HTML在构建应用上的不足而设计的.(引用百度百科) AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷.Angu ...
- JS表单提交的几种方式
第一种方式 : 表单提交,在 form 标签中增加 onsubmit 事件来判断表单是否提交成功 <script type="text/javascript"> fun ...
随机推荐
- Android Viewpager PagerAdapter update data 刷新界面数据
最近做的项目涉及到ViewPager数据刷新,网上的资料挺多,但是和现在做的这个不太相同,所以并没有找到有效的. 折腾了大半天,整理一下思路: 问题1: 后台刷新数据次数过多后,界面出现卡顿现象,判断 ...
- r语言入门资料
最近有好多r语言的爱好者问我r语言的事情,在百度上简单的收一收,感觉都在扯淡,真正适合初学者入门的资料几乎没有,比如最开始用什么编辑器比较好,在哪下载,最开始学习的例子有什么? 在日本网站上反倒是找 ...
- java 内部类的使用
http://www.cnblogs.com/wenruo/p/5387995.html 内部类 就是在类中嵌套的另一个类. 非静态内部类 创建内部类的方式就是把类定义在外部类里面. class Ou ...
- Android实例-使用自定义字体文件(XE8+小米2)
结果: 1.需要修改DELPHI自身的FMX.FontGlyphs.Android.pas,复制到程序的根目录下(红色部分为修改过的). 2.字体文件从 C:\Windows\Fonts 直接拷贝到A ...
- 第十三章、学习 Shell Scripts 循环 (loop)
while do done, until do done (不定循环) while [ condition ] <==中括号内的状态就是判断式 do <==do 是回圈的开始! 程序段落 ...
- 基于UDP协议的多路广播地址
因为多路广播地址的范围是224.0.0.0 到239.255.255.255之间,所以我们可以从中取一个地址给我们的程序用即可
- 转载ASP.NET MVC 和ASP.NET Web Form简单区别
转载原地址 http://www.cnblogs.com/lei2007/p/3315431.html 概论: Asp.net 微软 提供web开发框架或者技术.分Web Form和ASP.NET ...
- jQuery插件- Autocomplete应用详解
项目中有时会用到自动补全查询,就像Google搜索框.淘宝商品搜索功能,输入汉字或字母,则以该汉字或字母开头的相关条目会显示出来供用户选择, autocomplete插件就是完成这样的功能. auto ...
- MySQL 约束
MySQL中约束保存在information_schema数据库的table_constraints中,可以通过该表查询约束信息: 约束主要完成对数据的检验,保证数据库数据的完整性:如果有相互依赖数据 ...
- 如何在线缩小jpg图片的大小
直接使用在线PS保存成更小格式,即可! 在线PS网址:http://www.webps.cn/ 打开图片 点击保存 直接拖动滑动条就可以改变图片大小