XStream Quick Start

32
XStream Quick Start Albert Guo [email protected]

Transcript of XStream Quick Start

XStream Quick StartAlbert Guo

[email protected]

© 2006 PTC2

Agenda

Brief Introduction to XStream

What is XStream

XStream Features

Official Site

FAQ

Use Cases

Use Case 1: JavaBean To XML

Use Case 2 : XML To JavaBean

Using Alias

Using Implicit Collection

© 2006 PTC3

What is XStream

XStream is a simple library to serialize objects to XML and back again.

serialize objects XML

© 2006 PTC4

XStream Features (1/2)

Ease of use

A high level facade is supplied that simplifies common use cases.

No mappings required

Most objects can be serialized without need for specifying mappings.

Performance

Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.

Clean XML

No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.

.

© 2006 PTC5

XStream Features (2/2)

Requires no modifications to objects

Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.

Full object graph support

Duplicate references encountered in the object-model will be maintained. Supports circular references.

Integrates with other XML APIs

By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).

Customizable conversion strategies

Strategies can be registered allowing customization of how particular types are represented as XML.

Error messages

When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem

© 2006 PTC6

Official Site

http://xstream.codehaus.org/index.html

© 2006 PTC7

Quick Start

© 2006 PTC8

XStream FAQ

Use Case 1: JavaBean To XML

serialize objects XML

© 2006 PTC10

Use Case 1: JavaBean To XML

1. Prepared two persons’ contact information, including first name, last name phone call and fax number

2. System should write these contact information into Person.xml correctly

serialize objects XML

© 2006 PTC11

Four Steps

© 2006 PTC12

Step1. Creating classes to be serialized

© 2006 PTC13

Step2. Preparing data

© 2006 PTC14

Step3. Initializing XStream

© 2006 PTC15

Step4. Serializing an object to XML and writing XML object into file

Xs tre am.ra r

Source Code

Use Case 2 : XML To JavaBean

serialize objects XML

© 2006 PTC17

Use Case 2 : XML To JavaBean

1. Read from Person.xml

2. Parse the xml file and put contact info into Person bean

XML To JavaBean

serialize objects XML

© 2006 PTC18

Four Steps

Using Alias

© 2006 PTC20

Alias

Based on the xml file above we shall create some model classes and configure XStream to write/read from this format.

© 2006 PTC21

Step1. Creating classes to be serialized

© 2006 PTC22

Step2. Preparing data

© 2006 PTC23

Step3. Initializing XStream and serializing an object to XML

© 2006 PTC24

Step3. Initializing XStream and serializing an object to XML

© 2006 PTC25

Use Alias

Using Implicit Collection

© 2006 PTC27

Using Implicit Collection

Now let's implement what was called an implicit collection: whenever you have a collection which doesn't need to display it's root tag, you can map it as an implicit collection.

In our example, we do not want to display the orderList tag, but simply show the Order tags one after another.

A simple call to the addImplicitCollection method shall configure XStream and let it know that we do not want to write the orderList tag as described above:

© 2006 PTC28

The Model

© 2006 PTC29

Simple Test

© 2006 PTC30

Simple Test

© 2006 PTC31

Simple Test

© 2006 PTC32

Result