JAXB(3)xsd 验证

JAXB(三)xsd 验证

现在只有最简单的关联映射验证

关键点:jaxbMarshaller.setSchema( sch );

还不会验证集合类型:List,Set,Map,以后再把JAXB(二)的例子加上,

 

 

/**
 * @author timeriver.wang
 * @date 2014-03-06 0:57:01 AM
 */
@XmlRootElement
public class Address {
    private String area;
    
    private String street;
    
    @XmlElement(name = "area")
    public String getArea() {
        return area;
    }

    public void setArea( String area ) {
        this.area = area;
    }

    @XmlElement(name = "street")
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
}

 

 

/**
 * @author timeriver.wang
 * @date 2014-03-06 0:57:32 AM
 */
// @XmlRootElement(namespace ="NAMESPACE" )
@XmlRootElement
@XmlType(propOrder = {"name","mobile","address"})
public class Teacher {

    private String name;
    
    private String mobile;
    
    private Address address;

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }

    public void setName( String name ) {
        this.name = name;
    }

    @XmlElement(name = "mobile")
    public String getMobile() {
        return mobile;
    }

    public void setMobile( String mobile ) {
        this.mobile = mobile;
    }

    @XmlElement(name = "address")
    public Address getAddress() {
        return address;
    }

    public void setAddress( Address address ) {
        this.address = address;
    }


}

  teacher.xsd

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- ************* -->
  <!-- Shared Types -->
  <!-- ************* -->

  <xs:simpleType name="string_phone">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9\-]{11,12}" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="string_not_empty">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"></xs:minLength>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="address">
    <xs:all>
      <xs:element minOccurs="0" name="area" type="string_not_empty" />
      <xs:element minOccurs="1" name="street" type="string_not_empty" />
    </xs:all>
  </xs:complexType>

  <xs:element name="teacher">
    <xs:complexType>
      <xs:all>
        <xs:element name="name" type="xs:string" minOccurs="1" />
        <xs:element name="mobile" type="string_phone" minOccurs="0" />
        <xs:element name="address" type="address" minOccurs="1" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

 

/**
 * @author timeriver.wang
 * @date 2014-03-06 0:58:56 AM
 */
public class Test {
    private static String filePath = "D:/teacher.xml";
    private static SchemaFactory schFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
    private static Schema sch;
    public static void main( String[] args )throws Exception {
//        new File时,当前目录为工程根目录,"teacher.xsd" 等效于 "D:/workspace/wnj-ui/teacher.xsd"
//        "/jaxb/teacher.xsd" 等效于 "D:\jaxb\teacher.xsd"
//        sch = schFactory.newSchema( new File("teacher.xsd") );
        sch = schFactory.newSchema(Test.class.getResource( "/jaxb/teacher.xsd" ));
        toXml();
        toObj();
    }

    private static void toXml()throws Exception {
        Teacher teacher = initTeacher();
        //
        JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
        // marshal 整理,编列,元帅的意思
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setSchema( sch );
        // format, make every element keep a separate line. 
        jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );

        //output --> file
        File file = new File( filePath );
        jaxbMarshaller.marshal( teacher, file );
        //output --> console
        jaxbMarshaller.marshal( teacher, System.out );
    }
    
    private static void toObj()throws Exception {
        File file = new File( filePath );
        JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Teacher teacher = (Teacher) jaxbUnmarshaller.unmarshal( file );
        System.out.println( teacher.getName() );
    }
    
    private static Teacher initTeacher(){
        Teacher teacher = new Teacher();
        teacher.setName( "DAO SHI" );
        teacher.setMobile( "18912345678" );
        Address address = new Address();
        address.setArea( "bei jing, hai dian qu" );
        address.setStreet( "shang di 8 jie 88 hao" );
        teacher.setAddress( address );
        return teacher;
    }

}