简体   繁体   English

如何在 XStream 中禁用漂亮打印(空格/换行符)?

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

This is how I create XStream instance for XML:这就是我为 XML 创建 XStream 实例的方式:

XStream xstream = new XStream();

This is for JSON:这是用于 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?我如何要求 XStream 禁用漂亮的打印?

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();

With some help from the community, I've figured out the answer.在社区的一些帮助下,我找到了答案。

For XML you have to change the way how you serialize:对于 XML,您必须更改序列化方式:

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.对于 JSON,您只需更改 XStream 的创建方式。 So the initialization of the XStream is changed to:所以XStream的初始化改成:

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.请注意,使用了 4 参数 JsonWriter 构造函数。

使用紧凑型编写器在 xstream 上使用 marschal 方法

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

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):漂亮打印的默认行为来自 AbstractXmlDriver.createWriter() 方法(XStream 使用 XppDriver 作为其默认的分层流驱动程序,这扩展了 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.如果您想全局禁用漂亮打印(同时保留所有其他默认行为)并且只使用简单的 toXML(o) 方法而不是搞乱这里建议的其他每次使用选项,您可以按如下方式初始化您的 XStream 实例。 This overrides the above method with a CompactWriter instead.这将使用 CompactWriter 覆盖上述方法。

XStream xstream = new XStream(new XppDriver() {
    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        return new CompactWriter(out, getNameCoder());
    }
});

Create your XStream instance in this way:以这种方式创建您的 XStream 实例:

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:这是 Format 类的构造函数:

/**
 * 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查看JsonWriter的源代码以获取更多信息

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM