How can I remove StaxEventItemWriter <root> tag added by start/end Document methods?

烂漫一生 提交于 2020-05-23 17:23:06

问题


How can I remove StaxEventItemWriter tag added by start/end Document methods ?

It is added by default when I'm generating the XML file, So please can any one tell me how I can remove default root tag.

 Ex-
    <?xml version='1.0' encoding='UTF-8'?>
    <root>
    <ressourcespleiade date="2015-10-03 06:38:00.000">
    ---
    ---
    </..>

回答1:


I ended doing something like this:

/**
 * {@link StaxEventItemWriter} which write no root tag as written elements are root
 * @param <T> Type of the written elements
 *
 * @author francois.dupire
 */
public class NoRootStaxEventItemWriter<T> extends StaxEventItemWriter<T> {

    /*
     * Methods
     */
    @Override
    protected void startDocument(XMLEventWriter writer) throws XMLStreamException {
        XMLEventFactory factory = createXmlEventFactory();

        // write start document
        writer.add(factory.createStartDocument(getEncoding(), getVersion()));
    }

    @Override
    protected void endDocument(XMLEventWriter writer) { }

}

IMO, that's not really clean, but not really bad either. That way you can rely on every mecanism of the StaxEventItemWriter class but writing no root element.



来源:https://stackoverflow.com/questions/49110971/how-can-i-remove-staxeventitemwriter-root-tag-added-by-start-end-document-meth

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