JAva注記(一)概念及びJABX使用例
5595 ワード
一、概念__________________________________________________________________
注記xmlプロファイルと同様に、同じ情報が2つの場所に保存されないようにします.
@Override,@Deprescated(key,value)は省略しています
@Retention(RetentionPolicy.RUNTIME)キーが1つしかない場合、keyは省略できます
@SupressWarnings({"uncheck","unused"})カッコは配列タイプを表し、keyは1つのみ、省略
@XmlElement(name="id")構成パラメータ、キー値ペア
注釈は@interfaceで定義を宣言し、メタ注釈はjava.lang.annotationパッケージに4つあります.
二、JAXB例_______________
JAXB(Java Architecture for XML Binding)
注記xmlプロファイルと同様に、同じ情報が2つの場所に保存されないようにします.
@Override,@Deprescated(key,value)は省略しています
@Retention(RetentionPolicy.RUNTIME)キーが1つしかない場合、keyは省略できます
@SupressWarnings({"uncheck","unused"})カッコは配列タイプを表し、keyは1つのみ、省略
@XmlElement(name="id")構成パラメータ、キー値ペア
注釈は@interfaceで定義を宣言し、メタ注釈はjava.lang.annotationパッケージに4つあります.
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {//are to be documented by javadoc and similar tools by default
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {//
}
//@Retention : CLASS、RUNTIME SOURCE, 、JVM 。
// RUNTIME, API , SSH xml
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
//@Target , 、
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
二、JAXB例_______________
JAXB(Java Architecture for XML Binding)
/**
* @author timeriver.wang
* @date 2013-01-09 8:07:01 PM
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student {
private String id;
private String name;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* @author timeriver.wang
* @date 2013-01-09 8:07:09 PM
*/
@XmlRootElement
public class Teacher {
private String id;
private String name;
private List<Student>students;
@XmlAttribute (name = "tid")
public String getId() {
return id;
}
public void setId( String id ) {
this.id = id;
}
@XmlElement(name = "tname")
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
@XmlElement(name = "students")
public List<Student> getStudents() {
return students;
}
public void setStudents( List<Student> students ) {
this.students = students;
}
}
/**
* @author timeriver.wang
* @date 2013-01-09 8:08:15 PM
*/
public class Test {
public static void main( String[] args )throws Exception {
// toXml();
// toStu();
toXmls();
}
public static void toXml()throws Exception {
Student stu = new Student();
stu.setId( "007" );
stu.setName( "zhouxingxing" );
File file = new File( "D:/stu.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Student.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
jaxbMarshaller.marshal( stu, file );
jaxbMarshaller.marshal( stu, System.out );
}
public static void toStu()throws Exception {
File file = new File( "D:/stu.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Student.class );
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student stu = (Student) jaxbUnmarshaller.unmarshal( file );
System.out.println( stu );
}
public static void toXmls()throws Exception {
Student stu = new Student();
stu.setId( "007" );
stu.setName( "zhouxingxing" );
Student stu2 = new Student();
stu2.setId( "008" );
stu2.setName( "dawenxi" );
List<Student>students = new ArrayList<Student>();
students.add( stu );
students.add( stu2 );
Teacher teacher = new Teacher();
teacher.setId( "101" );
teacher.setName( "DASHI" );
teacher.setStudents( students );
File file = new File( "D:/teacher.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
jaxbMarshaller.marshal( teacher, file );
jaxbMarshaller.marshal( teacher, System.out );
}
}