Use xstream to serialize/deserialize XML as a Java object

Source: Internet
Author: User
The project needs to be translated. It is simple and convenient.

Http://xstream.codehaus.org/tutorial.html

Two minute tutorial

This is a very quick introduction to xstream. skim read it to get an idea of how simple it is to convert objects to XML and back again. i'm sure you'll have questions afterwards.

Create classes to be serialized

Here's a couple of simple classes. xstream can convert instances of these to XML and back again.

public class Person {  private String firstname;  private String lastname;  private PhoneNumber phone;  private PhoneNumber fax;  // ... constructors and methods}public class PhoneNumber {  private int code;  private String number;  // ... constructors and methods}

Note:Notice that the fields are private. xstream doesn't care about the visibility of the fields. No getters or setters are needed. Also, xstream does not limit you to having a default constructor.

Initializing xstream

To use xstream, simply instantiateXStreamClass:

XStream xstream = new XStream();

You requirexstream-[version].jarAndxpp3-[version].jarIn the classpath. xpp3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use a standard JAXP Dom parser instead:

XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

Note:This class is a simple facade designed for common operations. For more flexibility you may choose to create your own facade that behaves differently.

Now, to make the XML outputted by xstream more concise, you can create aliases for your custom class names to XML element names. This isOnlyType of mapping required to use xstream and even this is optional.

xstream.alias("person", Person.class);xstream.alias("phonenumber", PhoneNumber.class);

Note:This is an optional step. without it xstream wocould work fine, but the XML element names wocould contain the fully qualified name of each class (including package) which wowould bulk up the XML a bit. see the alias tutorial a more complete introduction.

Serializing an object to XML

Let's create an instance of person and populate its fields:

Person joe = new Person("Joe", "Walnes");joe.setPhone(new PhoneNumber(123, "1234-456"));joe.setFax(new PhoneNumber(123, "9999-999"));

Now, to convert it to XML, all you have to do is make a simple call to xstream:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

<person>  <firstname>Joe</firstname>  <lastname>Walnes</lastname>  <phone>    <code>123</code>    <number>1234-456</number>  </phone>  <fax>    <code>123</code>    <number>9999-999</number>  </fax></person>

It's that simple. Look at how clean the XML is.

Deserializing an object back from XML

To reconstruct an object, purely from the XML:

Person newJoe = (Person)xstream.fromXML(xml);

And that's how simple xstream is!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.