Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

XStream Quick Start

  • Be the first to comment

XStream Quick Start

  1. 1. XStream Quick Start Albert Guo [email_address]
  2. 2. Agenda <ul><li>Brief Introduction to XStream </li></ul><ul><ul><li>What is XStream </li></ul></ul><ul><ul><li>XStream Features </li></ul></ul><ul><ul><li>Official Site </li></ul></ul><ul><ul><li>FAQ </li></ul></ul><ul><li>Use Cases </li></ul><ul><ul><li>Use Case 1: JavaBean To XML </li></ul></ul><ul><ul><li>Use Case 2 : XML To JavaBean </li></ul></ul><ul><li>Using Alias </li></ul><ul><li>Using Implicit Collection </li></ul>© 2006 PTC
  3. 3. What is XStream <ul><li>XStream is a simple library to serialize objects to XML and back again. </li></ul>© 2006 PTC serialize objects XML
  4. 4. XStream Features (1/2) <ul><li>Ease of use </li></ul><ul><ul><li>A high level facade is supplied that simplifies common use cases. </li></ul></ul><ul><li>No mappings required </li></ul><ul><ul><li>Most objects can be serialized without need for specifying mappings. </li></ul></ul><ul><li>Performance </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>Clean XML </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>. </li></ul>© 2006 PTC
  5. 5. XStream Features (2/2) <ul><li>Requires no modifications to objects </li></ul><ul><ul><li>Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor. </li></ul></ul><ul><li>Full object graph support </li></ul><ul><ul><li>Duplicate references encountered in the object-model will be maintained. Supports circular references. </li></ul></ul><ul><li>Integrates with other XML APIs </li></ul><ul><ul><li>By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML). </li></ul></ul><ul><li>Customizable conversion strategies </li></ul><ul><ul><li>Strategies can be registered allowing customization of how particular types are represented as XML. </li></ul></ul><ul><li>Error messages </li></ul><ul><ul><li>When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem </li></ul></ul>© 2006 PTC
  6. 6. Official Site © 2006 PTC http://xstream.codehaus.org/index.html
  7. 7. Quick Start © 2006 PTC
  8. 8. XStream FAQ © 2006 PTC
  9. 9. Use Case 1: JavaBean To XML serialize objects XML
  10. 10. Use Case 1: JavaBean To XML <ul><li>Prepared two persons’ contact information, including first name, last name phone call and fax number </li></ul><ul><li>System should write these contact information into Person.xml correctly </li></ul>© 2006 PTC serialize objects XML
  11. 11. Four Steps © 2006 PTC
  12. 12. Step1. Creating classes to be serialized © 2006 PTC
  13. 13. Step2. Preparing data © 2006 PTC
  14. 14. Step3. Initializing XStream © 2006 PTC
  15. 15. Step4. Serializing an object to XML and writing XML object into file © 2006 PTC Source Code
  16. 16. Use Case 2 : XML To JavaBean serialize objects XML
  17. 17. Use Case 2 : XML To JavaBean <ul><li>Read from Person.xml </li></ul><ul><li>Parse the xml file and put contact info into Person bean </li></ul>© 2006 PTC XML To JavaBean serialize objects XML
  18. 18. Four Steps © 2006 PTC
  19. 19. Using Alias
  20. 20. Alias <ul><li>Based on the xml file above we shall create some model classes and configure XStream to write/read from this format. </li></ul>© 2006 PTC
  21. 21. Step1. Creating classes to be serialized © 2006 PTC
  22. 22. Step2. Preparing data © 2006 PTC
  23. 23. Step3. Initializing XStream and serializing an object to XML © 2006 PTC
  24. 24. Step3. Initializing XStream and serializing an object to XML © 2006 PTC
  25. 25. Use Alias © 2006 PTC
  26. 26. Using Implicit Collection
  27. 27. Using Implicit Collection <ul><li>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. </li></ul><ul><li>In our example, we do not want to display the orderList tag, but simply show the Order tags one after another. </li></ul><ul><li>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: </li></ul>© 2006 PTC
  28. 28. The Model © 2006 PTC
  29. 29. Simple Test © 2006 PTC
  30. 30. Simple Test © 2006 PTC
  31. 31. Simple Test © 2006 PTC
  32. 32. Result © 2006 PTC

×