How to pretty print xml with jackson/woodstox?

1.9k Views Asked by At

I have a xml input stream that contains all tags in one line, eg <xml><MyRequest><someProperty attribute="test"/></MyRequest></xml>

Question: how can I best output this pretty printed?

Using jackson and woodstox, it would be as follows if I had a simple java bean object:

ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(new MyRequest());

But I don't have any java bean and don't want to create one. Just pretty print the given xml without transformation to any bean.

How could I best achieve this, with regards to performance?

1

There are 1 best solutions below

0
On

This can be done by setting this property:

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
String reqXml = xmlMapper.writeValueAsString(new MyRequest());