Thomas Sundberg

January 19, 2010

How to convert a POJO to XML with JAXB

Filed under: Java — Tags: , , , , , , , — Thomas Sundberg @ 15:43

This post is available at


  1. What is the cost of using the annotations? For Instance, if I have my POJO’s and use them as input/output for web service calls but then make the EJB accessible only through RMI, how does this effect performance? Is there still overheard with the annotations on the POJO, or should the annotations be removed. The annotations are nice to have because we can easily unmarshal our response and view it in a nice format, but I’m not sure what the overhead is for our production environment where we are obtaining ~100k calls per hour.

    Comment by Keith S. — September 9, 2011 @ 16:51

    • Hi!

      Annotations are only used by tools to do some something with a class. Annotations doesn’t add overhead per se, it is what the tool will do with the annotations that will add overhead. The only reasonable way to measure if they will add any overhead that is problematic in your situation is of course to test with and without the annotations.

      Implement the same functionality using annotated classes and run a suit of tests that simulates say 5 hours worth of traffic. The do the same without the annotated solution and test the with same load. Is the difference significant? Use the solution that is best both from a performance perspective and a maintenance perspective.


      Comment by Thomas Sundberg — September 10, 2011 @ 06:18

  2. […] followed this example and it worked for single element of BillSummary and without having BillAllocation Set in my […]

    Pingback by CCE in calling JAXBContext.newInstance while marshalling hibernate objects to XML | Gravity Layouts — October 15, 2011 @ 13:33

  3. […] to the onedb engine. This is often not possible for legacy classes or conflicts with building ‘pure’ POJO objects. The onedb engine therefore allows defining nodes in the onedb cloud, which do not […]

    Pingback by onedb Architecture and Design « Missing Link — May 6, 2012 @ 00:35

  4. how do you compare xstream library with JAXB for converting POJO to XML ?

    Comment by Meyappan — May 24, 2012 @ 14:04

    • I haven’t done that. What is you experience?


      Comment by Thomas Sundberg — May 24, 2012 @ 14:35

      • i am developing a RESTful service which reads data from oracle database, and populates POJO using a framework like hibernate, and then POJO are converted to XML which will be the output from REST application which can be viewed using a browser by typing in a REST request URL. I was exploring other methods for POJO to XML conversion. it also works in a similar method to the JAXB library you have explained above. POJO are annotated with @xstreamAlias for xmlElement , @xstreamAsAttribute for xmlAttribute , and then the xstream API on the POJO will serialize them to xml output visible on the browser.

        i am still checking out where the hook or the web application configuration is specified so that the POJO is operated upon the configured xstream API’s.

        to my knowledge i am aware that the output from the Controller is a org.springframework.web.servlet.ModelAndView object which wraps the POJO which has to be serialized. hope i am making sense here.

        Comment by Meyappan — May 24, 2012 @ 15:46

  5. i also believe we can also add custom converters by extending com.thoughtworks.xstream.converters.Converter and overriding 2 methods – boolean canConvert(Class classType) and void marshal(Object value, writer, com.thoughtworks.xstream.converters.MarshallingContext context) to add custom xml serialization.

    also i think there is some integration between spring framework, and xstream, and finding many spring MVC applications using xstream. for example any class extending the org.springframework.web.servlet.view.xml.MarshallingView can use the org.springframework.oxm.xstream.XStreamMarshaller as it marshaller..

    let me know if you discover any more information regarding the same..

    Comment by Meyappan — May 24, 2012 @ 19:48

  6. sorry for the digression to xstream as it is not related to the main thread here, but this topic is related to POJO to xml conversion only using other methods..

    Comment by Meyappan — May 24, 2012 @ 19:50

  7. i have 2 queries related to the context/subject of this thread
    – what changes would be needed to write the POJO object to an output stream of a socket, or a servlet output stream , or just to write it directly to a file. in my case i would want the objects to be written to the output stream of the HttpServletResponse object.

    – what does @XmlAnyElement annotation inform the tool which is marshalling the object into xml. does it mean it is a collection or list object, and the tool has to iterate over the objects in the list, and invoke the marshaller on each of the list object ?

    Comment by Meyappan — May 24, 2012 @ 21:37

  8. the other thing i wanted to know is how to add formatting to the output xml. the string xml is printed out in one line. but i would want the output xml to be formatted and indented according to the hierarchy of the data.

    Comment by Meyappan — May 25, 2012 @ 13:22

  9. for example instead of printing a string xml like VolvoSedanOpelTruck

    we need to print it with formatting like below



    can we add a custom Writer /PrettyPrintWriter etc to customize how we need to write the xml, for example we need to customize flat file printing with a CData Section in the xml.

    Comment by Meyappan — May 25, 2012 @ 15:23

  10. sorry the previous xml message was formatted wrongly by the editor. so enclosing the xml with double codes.
    instead of printing a one line xml string like this – “VolvoSedanOpelTruck”

    we want to print like – ”



    ” to the output stream of a HttpServletResponse object.

    Comment by Meyappan — May 25, 2012 @ 15:28

  11. Did you ever think about fetching and reusing the JaxbContext for the objects, which must already exist in the Jersey framework if a call came from the client as a string and then got converted to a POJO … for those of us who want to do something like get a json-string message and then proxy it forward to another webservice as a xml-string message … it would be very cool to not have to create additional jaxbcontext objects for the conversion … thoughts?

    Comment by pulkitsinghal — August 18, 2012 @ 01:39

    • The Jersey framework is not used here. So no, I did not consider doing it that way.


      Comment by Thomas Sundberg — August 18, 2012 @ 08:50

  12. Nice tutorial, appreciate your effort

    Comment by umyangel — April 15, 2013 @ 23:14

  13. what about this line:
    public String convertToXml(Object source, Class… type) {

    whats the complete parameter Class… type?

    Comment by Rick — December 23, 2015 @ 21:17

    • Hi!

      Not really sure about your question.

      The parameter called ‘type’ may be of any class that is annotated as an XmlType so it can be transformed into a xml document.


      Comment by Thomas Sundberg — December 23, 2015 @ 21:40

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: