Shankh is London based consultancy started on 2011 to provide consultancy services primarily in Java. With several successful Java projects under our belt, we started exploring mobile based services towards end of 2012 which led to our first android/ios/windows-phone app which is staged for release in 2013

Some Random quotes


Innovation distinguishes between a leader and a follower. -Steve Jobs

It is not that i am smarter than others, i just persist with problems longer

Jaxb: Printing Xml with proper formatting

Ever wondered when we print xml, instead of printing it in single line can’t we print with proper formatting and indentation.  If you are using Jaxb then there is a  nice little trick in  to achieve that.


JAXBContext jc = JAXBContext.newInstance( "MyClass.class" );

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

marshaller.marshal( <object_to_marshal>, System.out );

Share

4 Responses to “Jaxb: Printing Xml with proper formatting”

  1. Don Says:

    works like a treat

  2. Anish Sneh Says:

    Hi

    Thanks for the suggestion, however I’m facing an issue for some JAXBs it just prints the PI for XML not complete XML.

    Please suggest.

    – Anish Sneh

  3. admin Says:

    Anish, I didn’t see your comment earlier. It seems like you haven’t annotated the class variables properly (most likely @XmlElement). Have a look at this tutorial for learning about Jaxb annotations https://jaxb.dev.java.net/tutorial/

  4. nishanth Says:

    works really good .. very useful for logging..

Leave a Reply

Subscribe to Comments?



RECENTPOSTS

MOSTCOMMENTS

MYARCHIVE