java 如何在 XStream 中禁用漂亮打印(空格/换行符)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable pretty-printing(white space/newline) in XStream?
提问by IgorM
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 禁用漂亮的打印?
采纳答案by IgorM
With some help from the community, I've figured out the answer.
在社区的一些帮助下,我找到了答案。
For XMLyou 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 JSONyou only change the way how the XStream is created. So the initialization of the XStream is changed to:
对于 JSON,您只需更改 XStream 的创建方式。所以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 构造函数。
回答by noclayto
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();
回答by Mork0075
Use the marschal method on xstream with a compact writer
使用紧凑型编写器在 xstream 上使用 marschal 方法
xstream.marshal(someObject, new CompactWriter(out));
回答by John Rix
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. This overrides the above method with a CompactWriter instead.
如果您想全局禁用漂亮打印(同时保留所有其他默认行为)并且只使用简单的 toXML(o) 方法而不是搞乱这里建议的其他每次使用选项,您可以按如下方式初始化您的 XStream 实例。这将使用 CompactWriter 覆盖上述方法。
XStream xstream = new XStream(new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new CompactWriter(out, getNameCoder());
}
});
回答by dieter
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 JsonWriterfor more information
检查源代码以JsonWriter获取更多信息
回答by h_ismaili
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);

