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 );


July 6th, 2009 at 5:10 pm
works like a treat
August 18th, 2010 at 1:03 am
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
September 25th, 2010 at 10:40 am
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/
June 7th, 2011 at 3:46 pm
works really good .. very useful for logging..