JAXB学习 (3)验证

JAXB学习 (三)验证

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

 

 

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);