XStream的简单使用
XStream
XStream是一個java對象和xml相互轉(zhuǎn)換的工具
創(chuàng)建XStream對象:XStream stream = new XStream()
Java對象轉(zhuǎn)換成xml:stream .toXML(...)
xml轉(zhuǎn)換成Java對象:stream .fromXML(...)
轉(zhuǎn)換裝配方式
DomDriver方式:new XStream(new DomDriver());
StaxDriver方式:new XStream(new StaxDriver());
XppDriver,默認方式,但是xpp方式經(jīng)常解析xml會出錯
Java轉(zhuǎn)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 //設(shè)置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轉(zhuǎn)換為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裝配方式創(chuàng)建XStream對象解析xml經(jīng)常會出錯:com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
Java轉(zhuǎn)JSON
xStream對JSON也有非常好的支持,它提供了2個模型驅(qū)動可以完成Java對象到JSON的相互轉(zhuǎn)換
1.使用JettisonMappedXmlDriver驅(qū)動,可以將Java對象轉(zhuǎn)換成json,也可以將json轉(zhuǎn)換為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實現(xiàn)與JSON的互相轉(zhuǎn)換
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轉(zhuǎn)換為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轉(zhuǎn)換為Json字符串
55 String json = stream.toXML(list);
56
57 Object obj = stream.fromXML(json);
58
59 System.out.println(obj);
60 }
61 }
2.使用JsonHierarchicalStreamDriver驅(qū)動,只能將Java對象轉(zhuǎn)換成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實現(xiàn)java轉(zhuǎn)換成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 *默認會給轉(zhuǎn)換后的對象添加一個根節(jié)點,可以通過重寫JsonHierarchicalStreamDriver的createWriter方法進行設(shè)置刪除額外添加的根節(jié)點
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 //默認會給轉(zhuǎn)換后的json添加一個根節(jié)點JSON,重寫JsonHierarchicalStreamDriver的createWriter方法可以設(shè)置刪除根節(jié)點
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 }
總結(jié)
以上是生活随笔為你收集整理的XStream的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动态添加html元素
- 下一篇: 动态引入/添加js脚本