Announcement Announcement Module
Collapse
No announcement yet.
StaxEventItemWriter: How to append to the existing file. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • StaxEventItemWriter: How to append to the existing file.

    HI,

    I need to read the records from table and write into an xml file. When I configure the job, the Reader with JdbcCursorItemReader and writer with StaxEventItemWriter. Every thing is working fine and I could see the output xml file is created successfully with all the content.

    Problem is when we go for Partition,
    Since We need to process some business logic, we went to the option of doing partitioning the job for which we have cutomized the reader to accept list of <records> and to process the same in writer.

    Reader partition is working fine. but writer is creating the output file only for the execution of first partition and it is getting failed from the second partition writer as "org.springframework.batch.item.ItemStreamException : File already exists:"

    do we have any configuration to append to the existing file?

    Code:
    staxItemWriter = new StaxEventItemWriter<Bean>();
    staxItemWriter.setResource(new FileSystemResource(outputPath+".xml"));
    staxItemWriter.setMarshaller(marshaller);
    staxItemWriter.setOverwriteOutput(false);
    staxItemWriter.setRootTagName(rootElement);
    staxItemWriter.open(new ExecutionContext());
    ...
    staxItemWriter.write(List<Bean>)
    please help me in this if this is acheivable..

    Awaiting for replies.
    Thanks,
    vijayt

  • #2
    Hi,

    I know this an old post but I hope this answer will be helpful.

    In fact, your misunderstand what append is in this context.

    Append means that you add some data at the end of your file.
    What you want is to add some XML tags inside your root tag, after the last existing tag.

    This is why you can find an append option in the FlatFileItemWriter but not in the StaxEventItemWriter.
    The solution is first to read the XML file data and then append the new ones to them before writing the updated XML file.

    Comment

    Working...
    X