Java object serialization with Xstream

It's an intresting tool. So I'd like to share some of the facts about it.

Xstream is used to convert java objects to xml and vice versa.
We need this kind of conversion because object persistance and
data transportation can be done very easily with xml.

-----------------------------------------------------------------------------------------
Object -> XML
by Using Xstream libs you can directly convert any object to xml doc.
String xml = xstream.toXML(Object o);

----------------------------------------------------------------
XML -> Object

XStream xs = new XStream(new DomDriver());
FileInputStream fis;
ObjectInputStream in = null;

fis = new FileInputStream("Path to the xml file");
xs.alias("person", Person.class); // give alias given to Person.class

try {
in = xs.createObjectInputStream(fis); // Convert happens here
} catch (IOException e) {
e.printStackTrace();
}

Person p1 = (Person) in.readObject();
Person p2 = (Person) in.readObject();
}


http://xstream.codehaus.org/

Comments

Popular posts from this blog

PostgreSQL bytea and oid

MySQL as Hive metadata store

Big Memory Go.