To create XML form Java objects is something that can be very error prone, e.g. doing the correct encoding of the various data types can be tricky. This is the reason why you never should do this by your own in Java. One of my favorite library to convert objects form and to XML is XStream . The XStream implementation is simple, strait forward and has a small footprint.
The following sample code is based on the current version 1.4.2 released in November 2011. It creates an instance of a class, serializes the object to XML and back again to a new object.