XStream

XStream是一个java对象和xml相互转换的工具

创建XStream对象:XStream stream = new XStream()
Java对象转换成xml:stream .toXML(…)
xml转换成Java对象:stream .fromXML(…)

转换装配方式

DomDriver方式:new XStream(new DomDriver());
StaxDriver方式:new XStream(new StaxDriver());
XppDriver,默认方式,但是xpp方式经常解析xml会出错

Java转XML

1.导入jar

xmlpull_1_0_5.jar
xpp3_min-1.1.4c.jar
xstream-1.4.4.jar

2.测试类

 1 public class TestC {
 2     /**
 3      * 控制台:
 4      *         xml:
 5      *         <list>
 6      *             <city id="1">
 7      *             <pid>1</pid>
 8      *             <cname>合肥</cname>
 9      *          </city>
10      *          <city id="2">
11      *            <pid>1</pid>
12      *            <cname>芜湖</cname>
13      *          </city>
14      *          <city id="3">
15      *            <pid>1</pid>
16      *            <cname>蚌埠</cname>
17      *          </city>
18      *          <city id="4">
19      *            <pid>1</pid>
20      *            <cname>淮北</cname>
21      *          </city>
22      *        </list>
23      */
24     @Test
25     public void test1() {//java--->xml
26         List<City> list = new ArrayList<>();
27         list.add(new City(1,1,"合肥"));
28         list.add(new City(2,1,"芜湖"));
29         list.add(new City(3,1,"蚌埠"));
30         list.add(new City(4,1,"淮北"));
31         
32         XStream stream = new XStream(new DomDriver());//DomDriver装配方式
33         
34         //设置id为City.class标签的属性<com.qf.pojo.City id="">
35         stream.useAttributeFor(City.class, "id");
36         
37         //使用别名city代替City.class    <com.qf.pojo.City> ----> <city> 
38         stream.alias("city", City.class);
39         
40         //list转换为xml字符串
41         String xml = stream.toXML(list);
42         
43         System.out.println("xml:
"+xml);
44     }
45     
46     /**
47      * 控制台:com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
48      */
49     @Test
50     public void test2() {//xml--->java
51         List<City> list = new ArrayList<>();
52         list.add(new City(1,1,"合肥"));
53         list.add(new City(2,1,"芜湖"));
54         list.add(new City(3,1,"蚌埠"));
55         list.add(new City(4,1,"淮北"));
56         
57         //默认装配方式,使用的其实是XppDriver
58         XStream stream = new XStream();
59         //会抛出异常:com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
60         String xml = stream.toXML(list);
61         
62         stream.processAnnotations(List.class);
63         Object fromXML = stream.fromXML(xml);
64         List city = List.class.cast(fromXML);
65         System.out.println("city:
"+city);
66     }
67     
68     /**
69      * 控制台:
70      * city:
71      * [City [id=1, pid=1, cname=合肥], City [id=2, pid=1, cname=芜湖], City [id=3, pid=1, cname=蚌埠], City [id=4, pid=1, cname=淮北]]
72      */
73     @Test
74     public void test3() {//xml--->java
75         List<City> list = new ArrayList<>();
76         list.add(new City(1,1,"合肥"));
77         list.add(new City(2,1,"芜湖"));
78         list.add(new City(3,1,"蚌埠"));
79         list.add(new City(4,1,"淮北"));
80         
81         XStream stream = new XStream(new DomDriver());
82         String xml = stream.toXML(list);
83         
84         stream.processAnnotations(List.class);
85         Object fromXML = stream.fromXML(xml);
86         List city = List.class.cast(fromXML);
87         System.out.println("city:
"+city);
88     }
89 }

xpp装配方式创建XStream对象解析xml经常会出错:com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser

Java转JSON

xStream对JSON也有非常好的支持,它提供了2个模型驱动可以完成Java对象到JSON的相互转换

1.使用JettisonMappedXmlDriver驱动,可以将Java对象转换成json,也可以将json转换为Java对象需要添加jettison.jar

 1 package com.qf.Test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.junit.Test;
 7 
 8 import com.qf.pojo.City;
 9 import com.thoughtworks.xstream.XStream;
10 import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
11 
12 /**
13  * 测试用JettisonMappedXmlDriver实现与JSON的互相转换
14  * @author 14505
15  *
16  */
17 public class TestJson {
18     /**
19      * Java--->json
20      * 控制台输出:
21      *         {"list":[{"com.qf.pojo.City":[{"id":1,"pid":1,"cname":"合肥"},{"id":2,"pid":1,"cname":"芜湖"}]}]}
22      */
23     @Test
24     public void test1() {
25         List<City> list = new ArrayList<>();
26         list.add(new City(1,1,"合肥"));
27         list.add(new City(2,1,"芜湖"));
28         
29         XStream stream = new XStream(new JettisonMappedXmlDriver());
30         
31         stream.setMode(XStream.NO_REFERENCES);
32 
33         //list转换为Json字符串
34         String json = stream.toXML(list);
35         
36         System.out.println(json);
37     }
38     
39     /**
40      * json--->Java
41      * 控制台输出:
42      *         [[City [id=1, pid=1, cname=合肥], City [id=2, pid=1, cname=芜湖]]]
43      */
44     @Test
45     public void test2() {
46         List<City> list = new ArrayList<>();
47         list.add(new City(1,1,"合肥"));
48         list.add(new City(2,1,"芜湖"));
49         
50         XStream stream = new XStream(new JettisonMappedXmlDriver());
51         
52         stream.setMode(XStream.NO_REFERENCES);
53 
54         //list转换为Json字符串
55         String json = stream.toXML(list);
56         
57         Object obj = stream.fromXML(json);
58         
59         System.out.println(obj);
60     }
61 }

2.使用JsonHierarchicalStreamDriver驱动,只能将Java对象转换成json

  1 package com.qf.Test;
  2 
  3 import java.io.Writer;
  4 import java.util.HashMap;
  5 import java.util.Map;
  6 
  7 import org.junit.Test;
  8 
  9 import com.qf.pojo.City;
 10 import com.thoughtworks.xstream.XStream;
 11 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 12 import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
 13 import com.thoughtworks.xstream.io.json.JsonWriter;
 14 
 15 /**
 16  * 测试用JsonHierarchicalStreamDriver实现java转换成JSON
 17  * @author 14505
 18  *
 19  */
 20 public class TestJson {
 21     /**
 22      * Java--->json
 23      * 控制台输出:
 24      *         JSON:{"map": [
 25      *              [
 26      *                "city1",
 27      *                {
 28      *                  "id": 1,
 29      *                  "pid": 1,
 30      *                  "cname": "合肥"
 31      *                }
 32      *              ],
 33      *              [
 34      *                "city2",
 35      *                {
 36      *                  "id": 2,
 37      *                  "pid": 1,
 38      *                  "cname": "芜湖"
 39      *                }
 40      *              ]
 41      *            ]}
 42      *默认会给转换后的对象添加一个根节点,可以通过重写JsonHierarchicalStreamDriver的createWriter方法进行设置删除额外添加的根节点
 43      */
 44     @Test
 45     public void test1() {
 46         City city1 = new City(1,1,"合肥");
 47         City city2 = new City(2,1,"芜湖");
 48         Map<String, City> map = new HashMap<>();
 49         map.put("city1", city1);
 50         map.put("city2", city2);
 51         
 52         XStream xStreamJ = new XStream(new JsonHierarchicalStreamDriver());
 53 
 54         String json = xStreamJ.toXML(map);
 55         System.out.println("JSON:"+ json);
 56     }
 57     
 58     /**
 59      * Java--->json
 60      * 控制台输出:
 61      *         {"map": [
 62      *              [
 63      *                "city1",
 64      *                {
 65      *                  "id": 1,
 66      *                  "pid": 1,
 67      *                  "cname": "合肥"
 68      *                }
 69      *              ],
 70      *              [
 71      *                "city2",
 72      *                {
 73      *                  "id": 2,
 74      *                  "pid": 1,
 75      *                  "cname": "芜湖"
 76      *                }
 77      *              ]
 78      *            ]
 79      *        }
 80      */
 81     @Test
 82     public void test2() {
 83         City city1 = new City(1,1,"合肥");
 84         City city2 = new City(2,1,"芜湖");
 85         Map<String, City> map = new HashMap<>();
 86         map.put("city1", city1);
 87         map.put("city2", city2);
 88         
 89         //默认会给转换后的json添加一个根节点JSON,重写JsonHierarchicalStreamDriver的createWriter方法可以设置删除根节点
 90         XStream xStreamJ = new XStream(new JsonHierarchicalStreamDriver() {
 91             @Override
 92             public HierarchicalStreamWriter createWriter(Writer out) {
 93                 return new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
 94             }
 95         });
 96 
 97         String json = xStreamJ.toXML(map);
 98         System.out.println("JSON:"+ json);
 99     }
100 }