JAXB(一)入门--概述,简单例子

JAXB(Java Architecture for XML Binding) 

主要方法:Marshaller的marshal()方法,

// marshal 整理,编列,元帅的意思

 

/**
 * @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(namespace ="NAMESPACE" )
@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;
    }
    @XmlElementWrapper(name="students")
    @XmlElement(name = "student")
    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 {
    private static String filePath = "D:/teacher.xml";
    public static void main( String[] args )throws Exception {
        toXml();
        toObj();
    }

    public static void toXml()throws Exception {
        // organize Object(to be saved/persisted)
        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( "daoyan" );
        teacher.setStudents( students );
        
        //
        JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
        // marshal 整理,编列,元帅的意思
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // 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 );
    }
    
    public 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() );
    }

}

 

你可能感兴趣的:(JAXB)