java中JAXB标准实现java对象与xml互转

首先需要对jaxb有个大概的了解,请参考 http://baike.baidu.com/link?url=IG4jQ8b3ywp1yqrNNJl0eBO4P8hf13VEpelbMQ-y6FddGca4Nu3y1ZkObRF1FrHR

然后,我们举一个小例子来看一下效果:

Student类:

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="student")
public class Student implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@XmlElement(name="id")
	private String studentNum;
	@XmlElement(name="username")
	private String stedentName;
	private ClassRoom classRoom;
	
	public String getStudentNum() {
		return studentNum;
	}
	public void setStudentNum(String studentNum) {
		this.studentNum = studentNum;
	}
	public String getStedentName() {
		return stedentName;
	}
	public void setStedentName(String stedentName) {
		this.stedentName = stedentName;
	}
	public ClassRoom getClassRoom() {
		return classRoom;
	}
	public void setClassRoom(ClassRoom classRoom) {
		this.classRoom = classRoom;
	}
	
	
}

ClassRoom类:

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "classRoom")
public class ClassRoom implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@XmlElement(name="id")
	private String roomNum;
	@XmlElement(name="className")
	private String roomName;
	
	public ClassRoom() {
		super();
	}

	public ClassRoom(String roomNum, String roomName) {
		super();
		this.roomNum = roomNum;
		this.roomName = roomName;
	}

	public String getRoomNum() {
		return roomNum;
	}

	public void setRoomNum(String roomNum) {
		this.roomNum = roomNum;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

}

JAXBContextTest测试类:

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


public class JAXBContextTest {

	/**
	 * @param args
	 * @throws JAXBException 
	 */
	public static void main(String[] args) throws JAXBException {
		JAXBContext ctx = JAXBContext.newInstance(Student.class);
		/*
		 * xml转java对象
		 */
		String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
						"<student>" +
							"<classRoom>" +
								"<className>计算机</className>" +
								"<id>1</id>" +
							"</classRoom>" +
							"<id>1</id>" +
							"<username>张三</username>" +
						"</student>";
		Unmarshaller um = ctx.createUnmarshaller();
		Student stu = (Student)um.unmarshal(new StringReader(xml));
		System.out.println(stu.getStudentNum()+","+stu.getStedentName());
		System.out.println(stu.getClassRoom().getRoomNum()+","+stu.getClassRoom().getRoomName());
		
		/*
		 * java对象转xml
		 */
		Marshaller marsh = ctx.createMarshaller();
		marsh.setProperty(Marshaller.JAXB_ENCODING,"gb2312");//编码格式   
		marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化生成的xml串   
		marsh.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>)   
		ClassRoom classRoom = new ClassRoom();
		classRoom.setRoomName("音乐");
		classRoom.setRoomNum("2");
		Student stud = new Student();
		stud.setClassRoom(classRoom);
		stud.setStudentNum("2");
		stud.setStedentName("李四"); 
		marsh.marshal(stud, System.out); 
		
	}

}

控制台打印结果:

1,计算机
1,张三
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<student>
    <id>2</id>
    <username>李四</username>
    <classRoom>
        <id>2</id>
        <className>音乐</className>
    </classRoom>
</student>





你可能感兴趣的:(java中JAXB标准实现java对象与xml互转)