关于JAXB的介绍,见:http://blog.csdn.net/qq_32786873/article/details/71715254
测试类:
package com.solin.test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.setName("邪恶小法师");
student.setAge("110");
student.setSex("男");
List list = new ArrayList<>();
Student.Friend f1 = new Student.Friend();
f1.setName("德玛西亚之力");
f1.setAge("888");
f1.setSex("男");
Student.Friend f2 = new Student.Friend();
f2.setName("无双剑姬");
f2.setAge("898");
f2.setSex("女");
list.add(f1);
list.add(f2);
student.setFriend(list);
//将java对象转换为XML字符串
String xmlStr = XmlAndJavaObjectConvert.convertToXml(student);
System.out.println(xmlStr);
//将xml字符串转换为java对象
System.out.println(XmlAndJavaObjectConvert.convertXmlStrToObject(Student.class, xmlStr));
}
}
实体类:
package com.solin.test;
import java.util.List;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
//XML文件中的根标识
@XmlRootElement(name = "Student")
//控制JAXB 绑定类中属性和字段的排序
@XmlType(propOrder = {
"name",
"age",
"sex",
"friend"
})
public class Student {
private String name;
private String age;
private String sex;
private List friend;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@XmlElementWrapper(name="friendList")
public List getFriend() {
return friend;
}
public void setFriend(List friend) {
this.friend = friend;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", friendList=" + friend + "]";
}
public static class Friend{
private String name;
private String age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Friend [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
}
工具类:
package com.solin.test;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class XmlAndJavaObjectConvert {
/**
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
/**
* 将String类型的xml转换成对象
*/
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
}
邪恶小法师
110
男
888
德玛西亚之力
男
898
无双剑姬
女
Student [name=邪恶小法师, age=110, sex=男, friendList=[Friend [name=德玛西亚之力, age=888, sex=男], Friend [name=无双剑姬, age=898, sex=女]]]