Jump to content

XStream

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Varnent (talk | contribs) at 04:18, 13 September 2015 (clean up per WP:CHECKWIKI, typo(s) fixed: For example → For example, using AWB). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

XStream Library
Developer(s)Codehaus
Initial releaseJanuary 1, 2004 (2004-01-01)
Stable release
1.4.8 / February 18, 2015 (2015-02-18)
Written inJava
Operating systemCross-platform
LicenseBSD-style license
Websitehttp://x-stream.github.io/

XStream is a Java library to serialize objects to XML (or JSON) and back again.[1]

XStream library

XStream uses reflection to discover the structure of the object graph to serialize at run time, and doesn't require modifications to objects. It can serialize internal fields, including private and final, and supports non-public and inner classes.[2]

Object graph serialization

When serializing an object it serializes the full object graph. Duplicate references encountered in the object-model will be maintained. For example, using the following class CD

package com.thoughtworks.xstream;
public class Cd {
	private String id;

	private Cd bonusCd;

	Cd(String id, Cd bonusCd) {
		this.id = id;
		this.bonusCd = bonusCd;
	}

	Cd(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public Cd getBonusCd() {
		return bonusCd;
	}
}

and add some of these object to a list

Cd bj = new Cd("basement_jaxx_singles");
Cd mr = new Cd("maria rita");
		
List order = new ArrayList();
order.add(mr);
// adds the same cd twice (two references to the same object)
order.add(bj);
order.add(bj);

// adds itself (cycle)
order.add(order);

XStream xstream = new XStream();
xstream.alias("cd", Cd.class);
System.out.println(xstream.toXML(order));

If the above code is executed with XStream's default relative references mode, it will generate the following XML:

<list>
  <cd>
    <id>maria rita</id>
  </cd>
  <cd>
    <id>basement_jaxx_singles</id>
  </cd>
  <cd reference="../cd[2]"/>
  <list reference=".."/>
</list>

XStream is free software, distributed under a permissive, revised BSD-style licence.

Usage

References

  1. ^ "Serializing Java Objects with XStream". XML.com, O'Reilly Media, Inc. 2004-08-18. Retrieved 2009-12-14.
  2. ^ "Use XStream to serialize Java objects into XML". Ibm.com. Retrieved 2009-12-14.
  3. ^ a b "XStream - References". Xstream.codehaus.org. Retrieved 2009-12-14.