JAXB学习三 (验证)

一个完整个对象-模型映射,一定少不了对验证的支持。

 

jaxb当然也支持验证,分为两个部分:marshall和unmarshall。

 

一、unmarshall端

unmarshall是从xml到java对象的过程,因此验证的是这个xml文件是不是有效的,要在unmarshall端使用验证非常简单:

Java代码 复制代码  收藏代码
  1. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();   
  2. unmarshaller.setSchema(SchemaFactory.newInstance(   
  3.         XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(   
  4.         new File("students.xsd")));   

从jaxb2.0开始,通过调用方法setSchema()的方式来指定验证,在2.0以前的版本里,通过调用setValidating()方法来指定验证:

Java代码 复制代码  收藏代码
  1. unmarshaller.setValidating(true)   

验证信息可以是一个schema文件,也可以是一个dtd文件(如果支持的话),要使用dtd的验证,则代码大致为:

Java代码 复制代码  收藏代码
  1. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();   
  2. unmarshaller.setSchema(SchemaFactory.newInstance(   
  3.         XMLConstants.RELAXNG_NS_URI).newSchema(   
  4.         new File("students.dtd")));  

一旦设置了验证信息,在进行unmarshell的时候,jaxb就会对读入的xml文件进行验证,例如有schema文件:

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.liulutu.com/students/"  
  3.     targetNamespace="http://www.liulutu.com/students/">  
  4.     <element name="students">  
  5.         <complexType>  
  6.             <sequence>  
  7.                 <element name="student" type="tns:StudentType" maxOccurs="unbounded" />  
  8.             </sequence>  
  9.         </complexType>  
  10.     </element>  
  11.     <simpleType name="SexType">  
  12.         <restriction base="string">  
  13.             <enumeration value="Male"></enumeration>  
  14.             <enumeration value="Female"></enumeration>  
  15.         </restriction>  
  16.     </simpleType>  
  17.   
  18.     <complexType name="StudentType">  
  19.         <attribute name="sex" type="tns:SexType"></attribute>  
  20.         <attribute name="name" type="string"></attribute>  
  21.     </complexType>  
  22. </schema>   

和符合这个schema的一个xml文件:

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>   
  2. <ns2:students xmlns:ns2="http://www.liulutu.com/students/">   
  3. </ns2:students>   

如果指定了需要验证,当根据上面的schema读入些xml时,就会抛出一个验证失败异常:

Java代码 复制代码  收藏代码
  1. [org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The content of element 'ns2:students' is not complete. One of '{student}' is expected.]   
  2.     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)   
  3.     at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)   
  4.     at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)   
  5.     at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)   
  6.     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)   
  7.     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)   
  8.     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)   
  9.     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)  

注:需要在unmarshall.unmarshall()方法调用之前设置验证信息。

二、marshall端

jaxb2.0中,marshall和unmarshall的验证设置是一样的:

Java代码 复制代码  收藏代码
  1. ObjectFactory factory = new ObjectFactory();   
  2. Students students = factory.createStudents();   
  3. Marshaller marshaller = jaxbContext.createMarshaller();   
  4. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   
  5. marshaller.setSchema(SchemaFactory.newInstance(   
  6.         XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(   
  7.         new File("students.xsd")));   
  8. marshaller.marshal(students, new File("a.xml"));   

和上面一样,会得到一个异常信息:

Java代码 复制代码  收藏代码
  1. [org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The content of element 'ns2:students' is not complete. One of '{student}' is expected.]   
  2.     at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)   
  3.     at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)   
  4.     at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)   

如果使用jaxb2.0之前的版本,则验证方式如下:

Java代码 复制代码  收藏代码
  1. Validator validator = jaxbContext.createValidator();   
  2. validator.validate(students);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值