How to disable pretty-printing(white space/newline) in XStream?

15,468

Solution 1

With some help from the community, I've figured out the answer.

For XML you have to change the way how you serialize:

The line:

xStream.toXML(o, new OutputStreamWriter(stream, encoding));

changed to line

xStream.marshal(o, new CompactWriter(new OutputStreamWriter(stream, encoding)));

For JSON you only change the way how the XStream is created. So the initialization of the XStream is changed to:

private final XStream xstreamOut = new XStream(new JsonHierarchicalStreamDriver() {
    public HierarchicalStreamWriter createWriter(Writer writer) {
        return new JsonWriter(writer, new char[0], "", JsonWriter.DROP_ROOT_MODE);
    }
});

Note that the 4-parameter JsonWriter constructor is used.

Solution 2

Thanks, your posts helped!!! Here is what I use to convert to a String.

String strXML = "";
XStream xs = new XStream();
StringWriter sw = new StringWriter();
xs.marshal(this,  new CompactWriter(sw));
strXML = sw.toString();

Solution 3

Use the marschal method on xstream with a compact writer

xstream.marshal(someObject, new CompactWriter(out)); 

Solution 4

The default behaviour of pretty-printing comes from the AbstractXmlDriver.createWriter() method (XStream uses XppDriver as its default hierarchical stream driver and this extends AbstractXmlDriver):

public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out, getNameCoder());
}

If you want to disable pretty-printing globally (whilst retaining all other default behaviours) and just use the simple toXML(o) method rather than messing about with the other per use options suggested here, you can initialise your XStream instance as follows. This overrides the above method with a CompactWriter instead.

XStream xstream = new XStream(new XppDriver() {
    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        return new CompactWriter(out, getNameCoder());
    }
});
Share:
15,468
IgorM
Author by

IgorM

say ".NET" and I'm sold

Updated on August 07, 2022

Comments

  • IgorM
    IgorM almost 2 years

    This is how I create XStream instance for XML:

    XStream xstream = new XStream();
    

    This is for JSON:

    private final XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
            public HierarchicalStreamWriter createWriter(Writer writer) {
                return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
            }
        });
    

    Both of them are pretty-printing (indenting) the output.

    How can I ask the XStream to disable the pretty printing?

  • IgorM
    IgorM almost 15 years
    What is the difference between "marshal" and "toXML"? This is what I use now: xStream.toXML(o, new OutputStreamWriter(stream, encoding));
  • IgorM
    IgorM almost 15 years
    Apparently the CompactWriter works only for XML. I've modified my command to the following one and now I'm getting always XML, even if I need JSON: xStream.marshal(o, new CompactWriter(new OutputStreamWriter(stream, encoding)));
  • Mork0075
    Mork0075 almost 15 years
    See JavaDoc - toXML: Serialize an object to a pretty-printed XML String. - marschal: Serialize and object to a hierarchical data structure (such as XML). So with marshal you get some output options. There doenst seam to be such an option for JSON.
  • Mady
    Mady over 12 years
    How to define one's own print Writer. pretty-Print Writer does not suffice my needs. I want newline after every attribute and element. How shall that be done?
  • AabinGunz
    AabinGunz over 10 years
    How can we add prolog or stylesheet using CompactWriter? I was using new Writer().write method to add prolog and stylesheets, but CompactWriter does not have this function.
  • user1428716
    user1428716 over 2 years
    this method does not work when the xml node contains an underscore -- it adds an extra underscore -- eg -- my_name gets converted into my__name