一、导入jar包
<dependency>
<groupid>com.thoughtworks.xstream</groupid>
<artifactid>xstream</artifactid>
<version>1.4.8</version>
</dependency>
二、重要注解说明
@xstreamalias 定义别名
@xstreamasattribute 定义为属性
@xstreamomitfield 忽略
@xstreamconverter 处理日期格式
@xstreamimplicit(itemfieldname = "roles") 处理list
三、实例
1、定义javabean
import java.util.date;
import java.util.list;
import com.thoughtworks.xstream.annotations.xstreamalias;
import com.thoughtworks.xstream.annotations.xstreamasattribute;
import com.thoughtworks.xstream.annotations.xstreamconverter;
import com.thoughtworks.xstream.annotations.xstreamimplicit;
import com.thoughtworks.xstream.annotations.xstreamomitfield;
import com.tzz.util.xml.dateconverter;
@xstreamalias("user")
public class user {
@xstreamasattribute
private integer id;
private string name;
@xstreamomitfield
private int age;
private string sex;
@xstreamconverter(value = dateconverter.class)
private date birthday = new date();
@xstreamimplicit(itemfieldname = "roles")
private list<role> roles;
public integer getid() {
return id;
}
public void setid(integer 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 string getsex() {
return sex;
}
public void setsex(string sex) {
this.sex = sex;
}
public list<role> getroles() {
return roles;
}
public void setroles(list<role> roles) {
this.roles = roles;
}
public date getbirthday() {
return birthday;
}
public void setbirthday(date birthday) {
this.birthday = birthday;
}
}
import com.thoughtworks.xstream.annotations.xstreamalias;
@xstreamalias("role")
public class role {
private integer id;
private string name;
public integer getid() {
return id;
}
public void setid(integer id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
2、转换工具类
import java.io.writer;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.set;
import org.dom4j.element;
import com.thoughtworks.xstream.xstream;
import com.thoughtworks.xstream.core.util.quickwriter;
import com.thoughtworks.xstream.io.hierarchicalstreamwriter;
import com.thoughtworks.xstream.io.xml.prettyprintwriter;
import com.thoughtworks.xstream.io.xml.xppdriver;
public class xstreamxmlutil {
/** xml转bean对象 */
@suppresswarnings("unchecked")
public static <t> t xmltobean(string xml, class<t> clazz) {
xstream xstream = new xstream();
xstream.registerconverter(new dateconverter());
xstream.autodetectannotations(true);
xstream.processannotations(clazz);
xstream.setclassloader(clazz.getclassloader());
return (t) xstream.fromxml(xml);
}
/** bean对象转xml */
public static string beantoxml(object bean) {
// return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + new xstream().toxml(bean);
xstream xstream = new xstream();
xstream.registerconverter(new dateconverter());
xstream.autodetectannotations(true);
return xstream.toxml(bean);
}
}
3、测试类
import java.util.arraylist;
import java.util.date;
import java.util.list;
import org.junit.test;
import com.tzz.test.util.xml.entity.role;
import com.tzz.test.util.xml.entity.user;
import com.tzz.util.xml.xstreamxmlutil;
public class testxstreamxmlutil {
@test
public void testbeantoxml() {
try {
user user = new user();
user.setid(1);
user.setname("test");
user.setage(20);
user.setsex("1");
user.setbirthday(new date());
role role = new role();
role.setid(1);
role.setname("角色1");
role role2 = new role();
role2.setid(2);
role2.setname("角色2");
list<role> roles = new arraylist<role>();
roles.add(role);
roles.add(role2);
user.setroles(roles);
string xml = xstreamxmlutil.beantoxml(user);
system.out.println(xml);
} catch (exception e) {
e.printstacktrace();
}
}
@test
public void testxmltobean() {
string xml =
"<user id='1'>"+
"<name>test</name>"+
"<sex>1</sex>"+
"<birthday>2016-03-10 16:12:46</birthday>"+
"<roles>"+
"<id>1</id>"+
"<name>角色1</name>"+
"</roles>"+
"<roles>"+
"<id>2</id>"+
"<name>角色2</name>"+
"</roles>"+
"</user>";
user user = xstreamxmlutil.xmltobean(xml, user.class);
system.out.println(user.getid() + "--" + user.getname());
list<role> roles = user.getroles();
for (role r : roles) {
system.out.println(r.getname());
}
}
}
4、测试结果
4.1、运行testbeantoxml方法
<user id="1">
<name>test</name>
<sex>1</sex>
<birthday>2016-03-10 17:35:41</birthday>
<roles>
<id>1</id>
<name>角色1</name>
</roles>
<roles>
<id>2</id>
<name>角色2</name>
</roles>
</user>
4.2、运行testxmltobean方法
1--test
角色1
角色2
以上就是xstream实现bean与xml互转的代码示例的详细内容。