XStream轻松搞定JAVA对象与XMLJSON之间相互转换

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

XStream轻松搞定JAVA对象与XMLJSON之间相互转换
上一遍已对xstream进行了初步认识,此处就不再多作介绍,直入主题,以下主要主要为,JavaBean --> XML / JSON 、XML / JSON --> JavaBean之间相互转换:
一| 先写实体类:
Address.java
Java代码
1.public class Address {
2.private String oldAddress;
3.private String newAddress;
4.
5.public String getOldAddress() {
6.return oldAddress;
7.}
8.public void setOldAddress(String oldAddress) {
9.this.oldAddress = oldAddress;
10.}
11.public String getNewAddress() {
12.return newAddress;
13.}
14.public void setNewAddress(String newAddress) {
15.this.newAddress = newAddress;
16.}
17.
18.
Person.java
Java代码
1.public class Person {
2.private String name;
3.private String sex;
4.private int age;
5.private Address address;
6.
7.public String getName() {
8.return name;
9.}
10.public void setName(String name) {
= name;
12.}
13.public String getSex() {
14.return sex;
15.}
16.public void setSex(String sex) {
17.this.sex = sex;
18.}
19.public int getAge() {
20.return age;
21.}
22.public void setAge(int age) {
23.this.age = age;
24.}
25.public Address getAddress() {
26.return address;
27.}
28.public void setAddress(Address address) {
29.this.address = address;
30.}
31.
32.
Test.java
Java代码
1.import java.io.PrintWriter;
2.
3.import vo.Address;
4.import vo.Person;
5.
6.import com.thoughtworks.xstream.XStream;
7.import com.thoughtworks.xstream.io.json.JettisonMappe dXmlDriver;
8.import com.thoughtworks.xstream.io.json.JsonHierarchica lStreamDriver;
9.import com.thoughtworks.xstream.io.xml.DomDriver;
10.
11./**
12.*@class Description:
13.*@author like
14.*@version create on May 19, 2011
15.*/
16.public class Test {
17.public static void main(String[] args) {
18.//JavaBean --> XML
19.Address address=new Address();
20.Person person=new Person();
21.address.setOldAddress("湖南长沙");
22.address.setNewAddress("广州白云");
23.person.setName("理科");
24.person.setSex("男");
25.person.setAge(22);
26.person.setAddress(address);
27.XStream xstream = new XStream(new DomDriver());
28.xstream.alias("person", Person.class);
29.xstream.alias("address", Address.class);
30.xstream.toXML(person,new PrintWriter(System.out));
31.
32.//XML --> JavaBean
33.String strxml="<person><name>理科</name><sex>男</sex><age>19</age><address><oldAddress>湖南长沙</oldAddress><newAddress>广州白云</newAddress></address></person>";
34.XStream xstream2 = new XStream(new DomDriver());
35.xstream2.alias("person", Person.class);
36.xstream2.alias("address", Address.class);
37.Person person2=(Person) xstream2.fromXML(strxml);
38.System.out.println(person2.getName());
39.System.out.println(person2.getSex());
40.System.out.println(person2.getAddress().getOldAddre ss());
41.System.out.println(person2.getAddress().getNewAddr ess());
42.
43.//JavaBean --> JSON
44.Address address3=new Address();
45.Person person3=new Person();
46.address3.setOldAddress("湖南长沙");
47.address3.setNewAddress("广州白云");
48.person3.setName("理科");
49.person3.setSex("男");
50.person3.setAge(22);
51.person3.setAddress(address3);
52.//new JsonHierarchicalStreamDriver()
53.XStream xstream3 = new XStream(new JsonHierarchic alStreamDriver());
54.xstream3.alias("person", Person.class);
55.xstream3.alias("address", Address.class);
56.//new PrintWriter(System.out) 输出去控制台
57.xstream3.toXML(person3,new PrintWriter(System.out)) ;
58.
59.//JSON--> JavaBean
60.String jsonstr="{\"person\": {\"name\": \"理科\",\"sex\": \"男\",\"age\": \"19\",\"address\": {\"oldAddress\": \"湖南长沙\",\"newAddress\": \"广州白云\"}}}";
61.//new JettisonMappedXmlDriver()
62.XStream xstream4 = new XStream(new JettisonMappe dXmlDriver());
63.xstream4.alias("person", Person.class);
64.xstream4.alias("address", Address.class);
65.Person person4=(Person) xstream4.fromXML(jsonstr);
66.System.out.println(person4.getName());
67.System.out.println(person4.getSex());
68.System.out.println(person4.getAddress().getOldAddre ss());
69.System.out.println(person4.getAddress().getNewAddr ess());
70.}
71.}
注:
1)、使用xStream.alias(String elementName, Class cls)为任何一个自定义类创建到类到元素的别名,如果不使用别名,则生成的标签名为类全名;
2)、使用xStream.toXML(Object obj)转换对象到XML;
3)、使用xStream.fromXML(String xml)转换XML到对象;。

相关文档
最新文档