Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Serializing Java objects to XML and back

, 5 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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 … Continue reading →

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</span /> static</span /> String</span /> ObjectToXml(Object</span /> obj)

{

	String</span /> xmlStr;

	XStream xstream = new</span /> XStream();

	xmlStr = xstream.toXML(obj);

	return</span /> xmlStr;

}

public</span /> static</span /> Object</span /> XmlToObject(String</span /> xmlEncoded)

{

	Object</span /> recreated;

	XStream xT = new</span /> XStream();

	recreated = xT.fromXML(xmlEncoded);

	return</span /> 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 http://xstream.codehaus.org/.

License

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

Share

About the Author

Mark Sinnathamby
Software Developer Air Liquide Industrial Services
Singapore Singapore
Mark is a software developer at Air Liquide Industrial Services - Singapore, where he helps Develop, Deploy, and Maintain large scale enterprise applications in the Asia-Pacific region. While his main work experience converges on .Net technologies, he has proficient experience in C++, HTML5/CSS/Javascript Web technologies, mobile development, OpenGL and DirectX API's, as well as a bit of J2EE. He loves to dabble with Game programming in his spare time, and blogs about his development experience at http://markfaction.wordpress.com and http://gamecoderlogic.wordpress.com.
Follow on   Twitter

Comments and Discussions

 
GeneralFormatting issue Pinmemberdaveauld3-Sep-10 6:16 

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

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

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