XStream实现Bean与xml互转的代码示例

一、导入jar包

com.thoughtworks.xstreamxstream1.4.8

登录后复制

 二、重要注解说明

@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 {@XStreamAsAttributeprivate Integer id;private String name;@XStreamOmitFieldprivate int age;private String sex;@XStreamConverter(value = DateConverter.class)private Date birthday = new Date();@XStreamImplicit(itemFieldName = "roles")private List 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 getRoles() {return roles;}public void setRoles(List 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 xmlToBean(String xml, Class 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 "" + 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 {@Testpublic 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 roles = new ArrayList();roles.add(role);roles.add(role2);user.setRoles(roles);String xml = XStreamXmlUtil.beanToXml(user);System.out.println(xml);} catch (Exception e) {e.printStackTrace();}}@Testpublic void testXmlToBean() {String xml =   ""+  "Test"+  "1"+  "2016-03-10 16:12:46"+  ""+    "1"+    "角色1"+  ""+  ""+    "2"+    "角色2"+  ""+   "";User user = XStreamXmlUtil.xmlToBean(xml, User.class);System.out.println(user.getId() + "--" + user.getName());List roles = user.getRoles();for (Role r : roles) {System.out.println(r.getName());}}}

登录后复制

 4、测试结果

 4.1、运行testBeanToXml方法

  Test  1  2016-03-10 17:35:41      1    角色1        2    角色2  

登录后复制

  4.2、运行testXmlToBean方法

1--Test角色1角色2

登录后复制

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2418202.html

(0)
上一篇 2025年3月3日 02:13:41
下一篇 2025年3月3日 02:13:57

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论