Skip to content

Instantly share code, notes, and snippets.

@jeremie-lesage
Created March 9, 2012 10:00
Show Gist options
  • Save jeremie-lesage/2005911 to your computer and use it in GitHub Desktop.
Save jeremie-lesage/2005911 to your computer and use it in GitHub Desktop.
Xstream in two minute
/** http://xstream.codehaus.org/tutorial.html **/
public class DemoXstreal {
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
}
public static void main(String[] args) {
XStream xstream = new XStream(new StaxDriver());
// Serializing an object to XML
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
//Deserializing an object back from XML
Person newJoe = (Person)xstream.fromXML(xml);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment