问题
I want to append content to xml using xstream but using xstream.toXml(obj,writer) every time I run this, it is adding xml declaration to the content, can we omit xml declaration in xstream?
回答1:
In a meantime the XStream team is fixing the issue you may do workaround. Just override the createStaxWriter
in StaxDriver
:
StaxDriver driver = new StaxDriver() {
@Override
public StaxWriter createStaxWriter(XMLStreamWriter out) throws XMLStreamException {
//the boolean parameter controls the production of XML declaration
return createStaxWriter(out, false);
}
};
XStream xstream = new XStream(driver);
回答2:
I just had this issue (using XStream 1.4.4). The problem occurs if you use the StaxDriver instead of the JAXP DOM one or the default XPP3 one. So either switch to:
XStream xstream = new XStream();
(with the XPP3 and xmlpull JARs on the classpath) or
XStream xstream = new XStream(new DomDriver());
instead of
XStream xstream = new XStream(new StaxDriver());
This is presumably a (consistency) bug in XStream, since its FAQ explicitly states that it is for writing snippets and doesn't produce the XML declaration (see also this question). It provides no methods/settings to not produce it because it should never be produced! I will look into raising it with them.
来源:https://stackoverflow.com/questions/15622029/omitting-xml-declaration-in-xstream