我是靠谱客的博主 潇洒豆芽,这篇文章主要介绍XStream实现Bean与xml互转的代码示例,现在分享给大家,希望可以做个参考。

一、导入jar包

复制代码
1
2
3
4
5
<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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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; } }
登录后复制
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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、转换工具类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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、测试类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<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
2
3
1--Test 角色1 角色2
登录后复制

以上就是XStream实现Bean与xml互转的代码示例的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是潇洒豆芽最近收集整理的关于XStream实现Bean与xml互转的代码示例的全部内容,更多相关XStream实现Bean与xml互转内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部