Search code examples
javaxmlxstream

How to indent XML with XStream?


I would like to be able to set the indentation to a number of spaces when storing with XStream. I have seen this SO post and I'm afraid it's not quite clear how to set the number of spaces to use. I seem to not be getting something. Could somebody kindly explain this and illustrate it with an example?


Solution

  • Following the example in the post you've linked to and the API, I came up with this code:

    char[] indent = new char[] { ' ', ' ', ' ', ' ' };
    BufferedOutputStream stdout = new BufferedOutputStream(System.out);
    xstream.marshal(object, new PrettyPrintWriter(new OutputStreamWriter(stdout), indent));