Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format XML generated by Xstream

Tags:

java

xml

xstream

I want to format the output XML generated by Xstream, to make it more readable. Currently, a newline is added after each element but I would like newline to be added after every attribute. Is there a way to do this?

Pretty Print Writer is used by default to format the output of the xml but this doesn't suffice for me. I want newline to be added after every

like image 957
Mady Avatar asked Jan 20 '12 14:01

Mady


People also ask

How do I format an XML file?

To access XML formatting options, choose Tools > Options > Text Editor > XML, and then choose Formatting.

What is XStream jar used for?

XStream's primary purpose is for serialization. It allows existing Java objects to be converted to clean XML and then restored again, without modifications. This is particularly useful as a form of persistence (no complicated database mappings required) or for sending objects over the wire.

Is XStream thread safe?

For most use cases, the XStream instance is thread-safe, once configured (there are caveats when using annotations) Clear messages are provided during exception handling to help diagnose issues. Starting with version 1.4.


1 Answers

XStream includes a PrettyPrintWriter

After building your XStream...

XStream xstream = //...whatever

Instead of:

// p is my object needing xml serialization
xstream.toXML(p)

Use something like this to make it pretty:

BufferedOutputStream stdout = new BufferedOutputStream(System.out);
xstream.marshal(p, new PrettyPrintWriter(new OutputStreamWriter(stdout)));
like image 175
dustmachine Avatar answered Oct 05 '22 23:10

dustmachine