Xml 转换对象以及对象转换xml应用XStream

需要导入的包和项目如下:


User.java代码: public class User { private String id; private String name ; private String age; private String pwd; public String getId() { return id; } public void setId(String id) { this.id = id; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", pwd=" + pwd + "]"; } } Test.java代码 package com.test; import com.test.domain.User; import com.thoughtworks.xstream.XStream; public class Test { public static void main(String[] args) { User user = new User(); user.setAge("11"); user.setId("1"); user.setName("zhangsan"); user.setPwd("123456"); XStream xml = new XStream(); String userXml = xml.toXML(user); System.out.println("object to xml"); System.out.println(userXml); System.out.println("====================================="); System.out.println("xml to object"); User u = (User) xml.fromXML(userXml); System.out.println(u); } } 运行结果: object to xml <com.test.domain.User> <id>1</id> <name>zhangsan</name> <age>11</age> <pwd>123456</pwd> </com.test.domain.User> ===================================== xml to object User [id=1, name=zhangsan, age=11, pwd=123456] 这里可以给输出的xml起一个别名: 例如:下免得测试代码: package com.test; import com.test.domain.User; import com.thoughtworks.xstream.XStream; public class Test { public static void main(String[] args) { User user = new User(); user.setAge("11"); user.setId("1"); user.setName("zhangsan"); user.setPwd("123456"); XStream xml = new XStream(); xml.alias("address", User.class); String userXml = xml.toXML(user); System.out.println("object to xml"); System.out.println(userXml); System.out.println("====================================="); System.out.println("xml to object"); User u = (User) xml.fromXML(userXml); System.out.println(u); } } 运行结果: object to xml <address> <id>1</id> <name>zhangsan</name> <age>11</age> <pwd>123456</pwd> </address> ===================================== xml to object User [id=1, name=zhangsan, age=11, pwd=123456] 这里我们将user这个对象起了一个别名为address,其他的修改也是同理,如何要给属性起别名就用 xml.aliasField("userid", User.class, "id"); 这里我们也可以用注解的方式来起别名: 测试代码如下: User.java代码 package com.test.domain; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("user") public class User { @XStreamAlias("userId") private String id; private String name ; private String age; private String pwd; public String getId() { return id; } public void setId(String id) { this.id = id; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", pwd=" + pwd + "]"; } } Test.java测试代码如下: package com.test; import com.test.domain.User; import com.thoughtworks.xstream.XStream; public class Test { public static void main(String[] args) { User user = new User(); user.setAge("11"); user.setId("1"); user.setName("zhangsan"); user.setPwd("123456"); XStream xml = new XStream(); xml.processAnnotations(User.class); String userXml = xml.toXML(user); System.out.println("object to xml"); System.out.println(userXml); System.out.println("====================================="); System.out.println("xml to object"); User u = (User) xml.fromXML(userXml); System.out.println(u); } } 测试结果如下: object to xml <user> <userId>1</userId> <name>zhangsan</name> <age>11</age> <pwd>123456</pwd> </user> ===================================== xml to object User [id=1, name=zhangsan, age=11, pwd=123456] @XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("user", User.class);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值