Sring Batch 自定义xmlWriter

public class CusXmlWriter extends StaxEventItemWriter {


    private String resPath;

    public CusXmlWriter(String resPath){
        this.resPath = resPath;
        init();
    }

    private FileSystemResource fileSystemResource;
    private void init(){
        fileSystemResource = new FileSystemResource("E:\\code\\user.txt");
        setResource(fileSystemResource);
        XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();

        setRootTagName("users");

        Map aliases = new HashMap<>();
        aliases.put("user", User.class);
        aliases.put("name", String.class);
        aliases.put("tag", String.class);
        aliases.put("phone", String.class);
        aliases.put("address", String.class);
        aliases.put("cardNum", String.class);
        aliases.put("time", String.class);

        aliases.put("people", People.class);
        xStreamMarshaller.setAliases(aliases);

        setMarshaller(xStreamMarshaller);

        /*xml写入的回调*/
        setHeaderCallback(new StaxWriterCallback() {
            XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
            @Override
            public void write(XMLEventWriter xmlEventWriter) throws IOException {
                try {
                    /*在开头插入注释*/
                    xmlEventWriter.add(xmlEventFactory.createComment("开始时间 2019"));
                } catch (XMLStreamException e) {
                    e.printStackTrace();
                }
            }
        });

        /*xml写完的回调*/
        setFooterCallback(new StaxWriterCallback() {
            XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
            @Override
            public void write(XMLEventWriter xmlEventWriter) throws IOException {
                try {
                    /*在结尾插入注释*/
                    xmlEventWriter.add(xmlEventFactory.createComment("结束时间 2019"));
                } catch (XMLStreamException e) {
                    e.printStackTrace();
                }
            }
        });


    }

}

你可能感兴趣的:(Sring Batch 自定义xmlWriter)