22

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?

Arjan
  • 22,808
  • 11
  • 61
  • 71
IgorM
  • 1,068
  • 2
  • 18
  • 29
  • 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? http://stackoverflow.com/questions/8943000/format-xml-generated-by-xstream – Mady Jan 23 '12 at 09:27

6 Answers6

19

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();
Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
noclayto
  • 160
  • 1
  • 6
  • 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 – user1428716 Nov 07 '21 at 21:01
13

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.

Arjan
  • 22,808
  • 11
  • 61
  • 71
IgorM
  • 1,068
  • 2
  • 18
  • 29
  • 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? – Mady Jan 23 '12 at 09:26
  • 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. – AabinGunz Nov 08 '13 at 08:49
4

Use the marschal method on xstream with a compact writer

xstream.marshal(someObject, new CompactWriter(out)); 
Mork0075
  • 5,895
  • 4
  • 25
  • 24
  • 1
    What is the difference between "marshal" and "toXML"? This is what I use now: xStream.toXML(o, new OutputStreamWriter(stream, encoding)); – IgorM May 21 '09 at 19:29
  • 1
    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))); – IgorM May 21 '09 at 19:33
  • 1
    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. – Mork0075 May 22 '09 at 11:03
3

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());
    }
});
John Rix
  • 6,271
  • 5
  • 40
  • 46
0

Create your XStream instance in this way:

XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
        public HierarchicalStreamWriter createWriter(Writer writer) {
            return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE, new JsonWriter.Format(new char[0], new char[0], 0));
        }
});

Here is a constructor of Format class:

/**
 * Create a new Formatter.
 * 
 * @param lineIndenter the characters used for indenting the line
 * @param newLine the characters used to create a new line
 * @param mode the flags for the format modes
 * @since 1.4
 */
public Format(char[] lineIndenter, char[] newLine, int mode) {
    this(lineIndenter, newLine, mode, new NoNameCoder());
}

Check a source code of JsonWriter for more information

alex
  • 8,904
  • 6
  • 49
  • 75
0

this worked for me :

XStream stream = new XStream(new StaxDriver());
stream.toXML(messages, out);
StringWriter out = new StringWriter();
String s = out.toString();
LOG.info(s);
h_ismaili
  • 86
  • 1
  • 2