Click here to Skip to main content
13,348,874 members (78,447 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 3 Sep 2010

Serializing Java Objects to XML and Back

Rate this:
Please Sign up or sign in to vote.
How to serialize Java objects to XML and back

XStream is a very simple and lightweight library that I came across when developing my final year project at University. It can be used for serializing objects into an XML string and vice-versa. XStream also supports JSON output as of the latest release. No object code has to be modified, and the library is a very simple one to use. A necessary requirement in my project was to serialize an object to XML and persist the string in an SQL server. When needed, the string would be retrieved and converted back to the specific object it represented.

For this purpose, I had two methods, one to serialize objects into XML and another to reconstruct the object from the XML. These methods are shown below (The XStream JAR file has to be referenced in your project. In Netbeans, you would need to right click the libraries package under your project, and choose ‘Add JAR’ in order to select the XStream JAR file):

public static String ObjectToXml(Object obj)
	String xmlStr;
	XStream xstream = new XStream();
	xmlStr = xstream.toXML(obj);
	return xmlStr;
public static Object XmlToObject(String xmlEncoded)
	Object recreated;
	XStream xT = new XStream();
	recreated = xT.fromXML(xmlEncoded);
	return recreated;

As can be seen above, the library is a very simple one to use, and is a good option if all you need to do is juggle between objects and XML strings in a very simple manner. The XStream library is open source, and can be downloaded from


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mark Vinod Sinnathamby
Technical Lead Exilesoft
Sri Lanka Sri Lanka
Mark is a Technical Lead at Exilesoft, whose passion lies in coding, mentoring, and fueling technical innovation. In the past, he has worked as a developer for a product engineering company, an ERP/Technical Consultant in a Fortune 500 conglomerate, and also as a senior engineer for a startup in the manufacturing and design space. His current areas of research revolve around Enterprise Architecture, Big Data, NoSQL Technology, and Machine Learning.

In his spare time, Mark experiments with (computer) game design/development, operating system internals, and compiler design. He also discusses and blogs about various topics surrounding software development, computer science, game programming, and mathematics, which can be read at Feel free to email or message him anytime and strike up a conversation.

You may also be interested in...

Comments and Discussions

GeneralFormatting issue Pin
daveauld3-Sep-10 6:16
memberdaveauld3-Sep-10 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 5 Sep 2010
Article Copyright 2010 by Mark Vinod Sinnathamby
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid