- 浏览: 977455 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
运乃强谦:
老哥,你确定这个wsdl 地址 可以访问?
[CXF] Server与Client实现方式五:HTTPS -
wangyudong:
由CXF实现的微服务需要有比较好的工具去测试RESTful A ...
[CXF] Server与Client实现方式四:JMS -
dengmiao:
JAXB学习三 (验证) -
panamera:
你好。可以提供maven pom配置是怎么配置的?不知道你使用 ...
[CXF] Server与Client实现方式四:JMS -
u010221220:
请问楼主一二三部分的代码都应该放在哪个函数体中。
使用JDI监听Java程序运行
一个完整个对象-模型映射,一定少不了对验证的支持。
jaxb当然也支持验证,分为两个部分:marshall和unmarshall。
一、unmarshall端
unmarshall是从xml到java对象的过程,因此验证的是这个xml文件是不是有效的,要在unmarshall端使用验证非常简单:
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new File("students.xsd")));
从jaxb2.0开始,通过调用方法setSchema()的方式来指定验证,在2.0以前的版本里,通过调用setValidating()方法来指定验证:
unmarshaller.setValidating(true)
验证信息可以是一个schema文件,也可以是一个dtd文件(如果支持的话),要使用dtd的验证,则代码大致为:
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(SchemaFactory.newInstance( XMLConstants.RELAXNG_NS_URI).newSchema( new File("students.dtd")));
一旦设置了验证信息,在进行unmarshell的时候,jaxb就会对读入的xml文件进行验证,例如有schema文件:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.liulutu.com/students/" targetNamespace="http://www.liulutu.com/students/"> <element name="students"> <complexType> <sequence> <element name="student" type="tns:StudentType" maxOccurs="unbounded" /> </sequence> </complexType> </element> <simpleType name="SexType"> <restriction base="string"> <enumeration value="Male"></enumeration> <enumeration value="Female"></enumeration> </restriction> </simpleType> <complexType name="StudentType"> <attribute name="sex" type="tns:SexType"></attribute> <attribute name="name" type="string"></attribute> </complexType> </schema>
和符合这个schema的一个xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:students xmlns:ns2="http://www.liulutu.com/students/"> </ns2:students>
如果指定了需要验证,当根据上面的schema读入些xml时,就会抛出一个验证失败异常:
[org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The content of element 'ns2:students' is not complete. One of '{student}' is expected.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
注:需要在unmarshall.unmarshall()方法调用之前设置验证信息。
二、marshall端
jaxb2.0中,marshall和unmarshall的验证设置是一样的:
ObjectFactory factory = new ObjectFactory(); Students students = factory.createStudents(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setSchema(SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new File("students.xsd"))); marshaller.marshal(students, new File("a.xml"));
和上面一样,会得到一个异常信息:
[org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The content of element 'ns2:students' is not complete. One of '{student}' is expected.] at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
如果使用jaxb2.0之前的版本,则验证方式如下:
Validator validator = jaxbContext.createValidator(); validator.validate(students);
发表评论
-
自定义Spring MVC中的数据绑定
2015-02-01 18:27 1432默认情况下,spring mvc的数据映射的实现是自动查找请 ... -
Java单例(Singleton)
2014-05-22 11:40 1856【译自:http://www.journaldev.com ... -
Java函数式编程学习二
2014-06-05 10:58 2623上一节里,介绍了一个函数接口: java.uti ... -
Java函数式编程学习一
2014-05-07 22:02 7300一、缺省方法 首先看一段用Java 8写的代码: ... -
快速排序的几种实现
2014-05-06 23:13 1615快速排序是最经典的 ... -
计算Fibonacci数列
2014-05-04 15:37 1602Fibonacci数列的定义如 ... -
(转)Java函数式编程系列文章
2014-04-11 16:52 701转载: Java函数式编程(一) Java函数式编程( ... -
Java中的动态代理
2014-04-02 15:11 2306在使用CXF的时候,尤其是创建针对REST或SOAP服务的 ... -
Java 7中的Path
2014-04-01 17:33 0package test; public ... -
HashMap对HashTable和ArrayList对Vector
2014-03-26 14:16 814有人面试总喜欢问比 ... -
Java国际化:BreakIterator
2014-03-21 11:51 2929【译自:http://tutorials.jenkov.c ... -
Java中使用StreamTokenizer
2014-03-21 09:44 3259按照Javadoc里的描述:StreamTokenizer ... -
Java中枚举的用法
2014-03-19 15:31 2751Java 5里新引用了枚举类型,这篇文章简单介绍一下它的基 ... -
Java线程类三
2014-03-06 11:18 1108一、Callable 最早创建线程要么是通过实现Runn ... -
Java线程类二
2014-03-05 14:21 1153一、java.util.concurrent.Exchan ... -
Java线程类一
2014-03-04 17:25 913一、java.util.concurrent.CountD ... -
基于Java的Dropbox文件操作
2014-01-22 11:15 4281Dropbox提供了基于各种类型的API和应用类型的开发工 ... -
使用特殊字符控制Console输出
2013-11-28 10:39 1026如果要在console上打印 Hello + 1到10,最简 ... -
控制JAXB的输入输出
2013-11-27 16:06 4059上一节介绍了如何在解析模型的时候构建模型之间的父子链,其实 ... -
JAXB中怎么构建对父对象的链接
2013-11-27 11:17 3528还是以在第一节介绍JAXB的schema为例: < ...
相关推荐
jaxb学习资料
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
NULL 博文链接:https://lijie-insist.iteye.com/blog/2160015
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
JAXB的HelloWorld源码,适合想学习JAXB技术的新手学习。
JAXB的安装包及插件
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
使用jaxb生成XML例子,含有例子和注解解析
jaxb-2.2.jar jaxb jax
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
jaxb最新版本2.2.6
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
现在很多maven仓库均没有此jar jaxb-api-2.3.0.jar jaxb-core-2.3.0.jar
JAXB2.0的使用方法和测试代码,作者根据使用测试得出的总结。