修改别人的。
package cn.wjj;
public class ClassRoom {
private int id;
private String name;
private int grade;
private String messageId;
public int getId() {
return id;
}
public void setId(int 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;
}
public ClassRoom(int id, String name, int grade,String messageId) {
super();
this.id = id;
this.name = name;
this.grade = grade;
this.messageId=messageId;
}
//无参够着函数一定需要,否则JXBContext无法正常解析。
public ClassRoom() {
super();
}
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
}
package cn.wjj;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private int id;
private String name;
private int age;
private String school;
private ClassRoom classRoom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ClassRoom getClassroom() {
return classRoom;
}
public void setClassroom(ClassRoom classroom) {
this.classRoom = classroom;
}
public Student(int id, String name, int age, ClassRoom classroom,String school) {
super();
this.id = id;
this.name = name;
this.age = age;
this.classRoom = classroom;
this.school=school;
}
//无参够着函数一定需要,否则JXBContext无法正常解析。
public Student() {
super();
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
package cn.wjj;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.*;
public class XmlUtil {
public static String toXML(Object obj) {
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
try {
JAXBContext context = JAXBContext.newInstance(valueType);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
package cn.wjj;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ClassRoom classroom = new ClassRoom(1, "软件工程", 4,"010");
Student student = new Student(101, "张三", 22, classroom, "1");
String s=XmlUtil.toXML(student);
System.out.println(s);
// String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><student><age>25</age><classroom><grade>5</grade><id>1</id>"
// + "<name>软件工程2</name></classroom><id>102</id><name>李四</name></student>";
Student stu2=XmlUtil.fromXML(s, Student.class);
System.out.println(stu2.getAge());
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<age>22</age>
<classroom>
<grade>4</grade>
<id>1</id>
<messageId>010</messageId>
<name>软件工程</name>
</classroom>
<id>101</id>
<name>张三</name>
<school>1</school>
</student>
22