XStream Quick Start Albert Guo [email_address]
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 PTC
What is XStream XStream is a simple library to serialize objects to XML and back again. © 2006 PTC serialize objects XML
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 PTC
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 PTC
Official Site © 2006 PTC http://xstream.codehaus.org/index.html
Quick Start © 2006 PTC
XStream FAQ © 2006 PTC
Use Case 1: JavaBean To XML serialize objects XML
Use Case 1: JavaBean To XML Prepared two persons’ contact information, including first name, last name phone call and fax number System should write these contact information into Person.xml correctly © 2006 PTC serialize objects XML
Four Steps © 2006 PTC
Step1. Creating classes to be serialized © 2006 PTC
Step2. Preparing data © 2006 PTC
Step3. Initializing XStream © 2006 PTC
Step4. Serializing an object to XML and writing XML object into file © 2006 PTC Source Code
Use Case 2 : XML To JavaBean serialize objects XML
Use Case 2 : XML To JavaBean Read from Person.xml Parse the xml file and put contact info into Person bean © 2006 PTC XML To JavaBean serialize objects XML
Four Steps © 2006 PTC
Using Alias
Alias Based on the xml file above we shall create some model classes and configure XStream to write/read from this format. © 2006 PTC
Step1. Creating classes to be serialized © 2006 PTC
Step2. Preparing data © 2006 PTC
Step3. Initializing XStream and serializing an object to XML © 2006 PTC
Step3. Initializing XStream and serializing an object to XML © 2006 PTC
Use Alias © 2006 PTC
Using Implicit Collection
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 PTC
The Model © 2006 PTC
Simple Test © 2006 PTC
Simple Test © 2006 PTC
Simple Test © 2006 PTC
Result © 2006 PTC

XStream Quick Start

  • 1.
    XStream Quick StartAlbert Guo [email_address]
  • 2.
    Agenda Brief Introductionto 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 PTC
  • 3.
    What is XStreamXStream is a simple library to serialize objects to XML and back again. © 2006 PTC serialize objects XML
  • 4.
    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 PTC
  • 5.
    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 PTC
  • 6.
    Official Site ©2006 PTC http://xstream.codehaus.org/index.html
  • 7.
  • 8.
  • 9.
    Use Case 1:JavaBean To XML serialize objects XML
  • 10.
    Use Case 1:JavaBean To XML Prepared two persons’ contact information, including first name, last name phone call and fax number System should write these contact information into Person.xml correctly © 2006 PTC serialize objects XML
  • 11.
    Four Steps ©2006 PTC
  • 12.
    Step1. Creating classesto be serialized © 2006 PTC
  • 13.
  • 14.
  • 15.
    Step4. Serializing anobject to XML and writing XML object into file © 2006 PTC Source Code
  • 16.
    Use Case 2: XML To JavaBean serialize objects XML
  • 17.
    Use Case 2: XML To JavaBean Read from Person.xml Parse the xml file and put contact info into Person bean © 2006 PTC XML To JavaBean serialize objects XML
  • 18.
    Four Steps ©2006 PTC
  • 19.
  • 20.
    Alias Based onthe xml file above we shall create some model classes and configure XStream to write/read from this format. © 2006 PTC
  • 21.
    Step1. Creating classesto be serialized © 2006 PTC
  • 22.
  • 23.
    Step3. Initializing XStreamand serializing an object to XML © 2006 PTC
  • 24.
    Step3. Initializing XStreamand serializing an object to XML © 2006 PTC
  • 25.
    Use Alias ©2006 PTC
  • 26.
  • 27.
    Using Implicit CollectionNow 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 PTC
  • 28.
    The Model ©2006 PTC
  • 29.
  • 30.
  • 31.
  • 32.