Format XML generated by Xstream
Asked Answered
D

3

6

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

Dimercaprol answered 20/1, 2012 at 14:36 Comment(1)
xstream.codehaus.org/manual-tweaking-output.htmlMason
V
4

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)));
Vocal answered 19/7, 2013 at 15:32 Comment(4)
So, how could I tell it to indent with, say... four spaces? I can't seem to be able to find a method for this? Is this supported?Spectacles
@Spectacles Use the alternative constructor that takes an indentation chararray, e.g. new PrettyPrintWriter(writer, new char[]{' ', ' ', ' ', ' '})Kaminski
@aetheria: Thanks, I came across exactly that solution myself.Spectacles
This does not work if you are using the Stax driver. Any other ideas?Odontograph
P
2

Take a look at their tutorial on tweaking the output.

Pedal answered 20/1, 2012 at 14:39 Comment(3)
I have seen that tutorial but it didn't help.Dimercaprol
Thank you @GáborLipták - the link has been fixedGusman
Wow. That was fast :DPsychologist
H
1

I've used this to :

        xstream= new XStream(new DomDriver());

But it's not so efficient than StaxDriver()

Hypophosphate answered 27/6, 2019 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.