omitting xml declaration in xstream

我只是一个虾纸丫 提交于 2020-01-04 07:19:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!