Java对象和xml之间转换
一、使用JAXB实现Java类和xml之间转换
1.通过Java类生产xml文件
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private Long id;
private String name;
private ClassRoom classRoom;
private List list;
public Student() {
}
public Student(Long id,String name,ClassRoom classRoom,List list)
{
this.id = id;
this.name = name;
this.classRoom = classRoom;
this.list = list;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ClassRoom getClassRoom() {
return classRoom;
}
public void setClassRoom(ClassRoom classRoom) {
this.classRoom = classRoom;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
public class ClassRoom {
private Long id;
private String name;
private int grade;
public ClassRoom() {
}
public ClassRoom(Long id,String name,int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
通过JAXB将对象转成xml文件
@Test
public void test01()
{
try {
JAXBContext ctx = JAXBContext.newInstance(Student.class);
Marshaller marshaller = ctx.createMarshaller();
List list = new ArrayList();
list.add(new ClassRoom(2L,"jb2011",2));
list.add(new ClassRoom(3L,"jb2011",3));
Student stu = new Student(11L,"fjia",new ClassRoom(1L,"jb2011",1),list);
marshaller.marshal(stu, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
2.将xml转换成Java对象
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<classRoom>
<grade>1</grade>
<id>1</id>
<name>jb2011</name>
</classRoom>
<id>11</id>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="classRoom">
<grade>2</grade>
<id>2</id>
<name>jb2011</name>
</list>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="classRoom">
<grade>3</grade>
<id>3</id>
<name>jb2011</name>
</list>
<name>fjia</name>
</student>
转换
@Test
public void test02()
{
try {
JAXBContext ctx = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Student student = (Student) unmarshaller.unmarshal(new File("src/student.xml"));
System.out.println(student.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
二、使用XStream实现Java类和xml之间的转换
1.Java类转换为xml
@Test
public void test03()
{
List list = new ArrayList();
list.add(new ClassRoom(2L,"jb2011",2));
list.add(new ClassRoom(3L,"jb2011",3));
Student stu = new Student(11L,"fjia",new ClassRoom(1L,"jb2011",1),list);
XStream xStream = new XStream();
xStream.alias("classRoom", ClassRoom.class);
xStream.alias("student", Student.class);
xStream.toXML(stu, System.out);
}
输出xml
<student>
<id>11</id>
<name>fjia</name>
<classRoom>
<id>1</id>
<name>jb2011</name>
<grade>1</grade>
</classRoom>
<list>
<classRoom>
<id>2</id>
<name>jb2011</name>
<grade>2</grade>
</classRoom>
<classRoom>
<id>3</id>
<name>jb2011</name>
<grade>3</grade>
</classRoom>
</list>
</student>
2.xml转换为Java类实例
@Test
public void test04() {
try {
XStream xStream = new XStream();
xStream.alias("classRoom", ClassRoom.class);
xStream.alias("student", Student.class);
Student stu = (Student)xStream.fromXML(new FileInputStream(new File("src/student.xml")));
System.out.println(stu.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}