Home > Software engineering >  Read CSV and write two differents xml files - best practices
Read CSV and write two differents xml files - best practices

Time:10-22

I have a batch that reads a .csv file and maps its data into a POJO. I need to write two XML files as output. Currently, to write these two files, I generated classes via XJC from their XSD.

I am writing from a FlatFileItemWriter (written in a text file which is useless) via a delegator. It is in this delegator that I write my two XML files. The batch works fine, I produce both XML files as output, no problem. However, I'm not satisfied because I'm producing a useless empty .txt file and I'm using a FlatFileItemWriter, that's not the way to do it.

@Bean
@StepScope
public FlatFileItemWriter<MetaCsv> csvWriter(MetaAgregator aggregator) {
    FlatFileItemWriter<MetaCsv> flatFileItemWriter = new FlatFileItemWriter<>();
    String outFilePath = "resources/data/esopetosae/output/out.txt";
    flatFileItemWriter.setResource(new FileSystemResource(outFilePath));
    flatFileItemWriter.setLineAggregator(aggregator);
    flatFileItemWriter.setShouldDeleteIfExists(true);
    flatFileItemWriter.setShouldDeleteIfEmpty(true);
    return flatFileItemWriter;
}

Aggregator:

@Bean
public MetaAgregator aggregator() {
    return new MetaAgregator ();
}

How can I make my Writer manage the writing of the two different XML files (different rootXML, different structure and data) while keeping the aggregator? Is it possible?

CodePudding user response:

The FlatFileItemWriter is designed to write flat files, not XML files.

In your case, you can use a CompositeItemWriter with two StaxEventItemWriters as delegates to generate two XML files.

CodePudding user response:

Bonjour,

Merci pour le retour.

Je vais essayer avec CompositeItemWriter et deux StaxEventItemWriter. Mais pourrais je utiliser les classes Java générées avec Xjc qui me permettent d'écrire les XmL actuellement ?

Cdt,

  • Related