JAXB学习 (三)验证
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( 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);
?
?
?
?