JAXB(三)xsd検証
6233 ワード
最も簡単な関連マッピング検証のみが使用されます.
キー:jaxbMarshaller.setSchema( sch );
集合タイプ:List,Set,Mapはまだ検証されていませんが,後でJAXB(二)の例を加えて,
teacher.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;
}
}