forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXStream.html
More file actions
262 lines (239 loc) · 7.83 KB
/
Copy pathXStream.html
File metadata and controls
262 lines (239 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999.html">
<head>
<title>XStream</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>XStream</h2>
<h3>Introduction</h3>
<p>
XStream is an open source library for serializing Java objects
to and from XML.
Compared to <a target="_blank" href="http://www.castor.org">Castor</a>,
another similar library,
it is simpler and requires less configuration.
XStream can be downloaded from <a target="_blank"
href="http://xstream.codehaus.org/">here</a>.
</p>
<h3>Example</h3>
<p>
The data in this example represents musical artists,
their recordings, and tracks on those recordings.
There is a Java Bean class for each of these kinds of data named
<a href="JSON/Artist.java.html">Artist</a>,
<a href="JSON/Recording.java.html">Recording</a> and
<a href="JSON/Track.java.html">Track</a>.
</p>
<p>
The example is built and run using Ant. For details, see
<a href="XStream/build.properties.html">build.properties</a> and
<a href="XStream/build.xml.html">build.xml</a>.
</p>
<p>
A few utility classes were written to simplify the code.
These include
<a href="JSON/ObjectUtil.java.html">ObjectUtil</a>,
<a href="JSON/SystemUtil.java.html">SystemUtil</a> and
</p>
<p>
Once armed with the preceding code, using XStream can be as simple
as the following code implemented as a JUnit 4 test case.
</p>
<pre><div class="code">package com.ociweb.demo;
import com.thoughtworks.xstream.XStream;
import java.util.*;
import org.junit.*;
import static com.ociweb.lang.SystemUtil.*;
import static org.junit.Assert.*;
public class XStreamTest {
private XStream xstream = new XStream();
@Before public void setup() {
// Setting up these aliases isn't necesssary,
// but it makes the XML look nicer.
// Otherwise the element names are fully-qualfied class names.
xstream.alias("artist", Artist.class);
xstream.alias("recording", Recording.class);
xstream.alias("track", Track.class);
}
/**
* This tests converting a object to XStream when the object
* has a field that holds a collection of references to other objects.
*/
@Test public void testObjectConvert() {
Artist a = new Artist("Regina Spektor");
a.addRecording("Soviet Kitch", 2003);
a.addRecording("Begin To Hope", 2006);
Object expected = a;
String xml = xstream.toXML(expected);
out(xml + "\n"); // for debugging
Object actual = xstream.fromXML(xml);
assertEquals(expected, actual);
}
/**
* This tests converting an array of objects to XStream when the objects
* have a field that holds a collection of references to other objects.
*/
@Test public void testArrayConvert() {
List<Artist> artists = new ArrayList<Artist>();
Artist a = new Artist("Deathcab For Cutie");
artists.add(a);
Recording r =
a.addRecording("We Have the Facts and We're Voting Yes", 2000);
a.addRecording("The Photo Album", 2001);
a.addRecording("You Can Play These Songs With Chords", 2002);
a.addRecording("Transatlanticism", 2003);
a.addRecording("Plans", 2005);
a = new Artist("Regina Spektor");
artists.add(a);
r = a.addRecording("Begin To Hope", 2006);
r.addTrack("20 Years Of Snow", 4);
r = a.addRecording("Soviet Kitch", 2003);
r.addTrack("Chemo Limo", 4);
r.addTrack("Somedays", 5);
Object[] expected = artists.toArray();
String xml = xstream.toXML(expected);
out(xml + "\n"); // for debugging
Object[] actual = (Object[]) xstream.fromXML(xml);
assertEquals(expected, actual);
}
/**
* This tests converting a collection to XStream.
*/
@Test public void testCollectionConvert() {
List<String> colors = new ArrayList<String>();
colors.add("red");
colors.add("green");
colors.add("blue");
List<String> expected = colors;
String xml = xstream.toXML(expected);
out(xml + "\n"); // for debugging
Object actual = xstream.fromXML(xml);
assertEquals(expected, actual);
}
}</div></pre>
<p>
Here's the XML that is produced by XStream in this example.
It's very readable!
Note the "reference" attributes that use relative
XPath expressions to avoid circular references.
</p>
<pre><div class="code"><artist>
<name>Regina Spektor</name>
<recordings>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>Soviet Kitch</title>
<year>2003</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>Begin To Hope</title>
<year>2006</year>
</recording>
</recordings>
</artist>
<object-array>
<artist>
<name>Deathcab For Cutie</name>
<recordings>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>We Have the Facts and We're Voting Yes</title>
<year>2000</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>The Photo Album</title>
<year>2001</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>You Can Play These Songs With Chords</title>
<year>2002</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>Transatlanticism</title>
<year>2003</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks/>
<title>Plans</title>
<year>2005</year>
</recording>
</recordings>
</artist>
<artist>
<name>Regina Spektor</name>
<recordings>
<recording>
<artist reference="../../.."/>
<tracks>
<track>
<recording reference="../../.."/>
<name>20 Years Of Snow</name>
<rating>4</rating>
</track>
</tracks>
<title>Begin To Hope</title>
<year>2006</year>
</recording>
<recording>
<artist reference="../../.."/>
<tracks>
<track>
<recording reference="../../.."/>
<name>Chemo Limo</name>
<rating>4</rating>
</track>
<track>
<recording reference="../../.."/>
<name>Somedays</name>
<rating>5</rating>
</track>
</tracks>
<title>Soviet Kitch</title>
<year>2003</year>
</recording>
</recordings>
</artist>
</object-array>
<list>
<string>red</string>
<string>green</string>
<string>blue</string>
</list></div></pre>
<h3>Configuration</h3>
<p>
An XStream object can be configured to output XML differently than
the default. Common examples include the following.
</p>
<ul>
<li>
Output a primitive field as an attribute instead of a child element.
<pre><div class="code">
xstream.useAttributeFor(MyClass.class, "fieldName");</div></pre>
</li>
<li>
Don't output anything for a given field.
<pre><div class="code">
xstream.omitField(MyClass.class, "fieldName");</div></pre>
</li>
</ul>
<br/><br/>
<hr />
<p style="text-align:center">
Copyright © 2007 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>