Created
October 30, 2020 10:16
-
-
Save fmbenhassine/bcc5154ff3befd144d0d45e217c02601 to your computer and use it in GitHub Desktop.
#SpringBatch CSV to XML https://stackoverflow.com/questions/64584364
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.springframework.batch.sample; | |
import javax.sql.DataSource; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import org.springframework.batch.core.Job; | |
import org.springframework.batch.core.JobParameters; | |
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | |
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | |
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | |
import org.springframework.batch.core.launch.JobLauncher; | |
import org.springframework.batch.item.file.FlatFileItemReader; | |
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | |
import org.springframework.batch.item.xml.StaxEventItemWriter; | |
import org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilder; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.io.FileSystemResource; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | |
import org.springframework.oxm.jaxb.Jaxb2Marshaller; | |
@Configuration | |
@EnableBatchProcessing | |
public class MyJob { | |
@Bean | |
public FlatFileItemReader<Person> itemReader() { | |
return new FlatFileItemReaderBuilder<Person>() | |
.name("personItemReader") | |
.resource(new FileSystemResource("persons.csv")) | |
.delimited() | |
.names("id", "name") | |
.targetType(Person.class) | |
.build(); | |
} | |
@Bean | |
public StaxEventItemWriter<Person> itemWriter() { | |
Jaxb2Marshaller marchaller = new Jaxb2Marshaller(); | |
marchaller.setClassesToBeBound(Person.class); | |
return new StaxEventItemWriterBuilder<Person>() | |
.name("personItemWriter") | |
.resource(new FileSystemResource("persons.xml")) | |
.marshaller(marchaller) | |
.rootTagName("persons") | |
.build(); | |
} | |
@Bean | |
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) { | |
return jobs.get("job") | |
.start(steps.get("step") | |
.<Person, Person>chunk(2) | |
.reader(itemReader()) | |
.writer(itemWriter()) | |
.build()) | |
.build(); | |
} | |
public static void main(String[] args) throws Exception { | |
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class); | |
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | |
Job job = context.getBean(Job.class); | |
jobLauncher.run(job, new JobParameters()); | |
} | |
@Bean | |
public DataSource dataSource() { | |
return new EmbeddedDatabaseBuilder() | |
.setType(EmbeddedDatabaseType.H2) | |
.addScript("/org/springframework/batch/core/schema-drop-h2.sql") | |
.addScript("/org/springframework/batch/core/schema-h2.sql") | |
.build(); | |
} | |
@XmlRootElement | |
public static class Person { | |
private int id; | |
private String name; | |
public Person() { | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String toString() { | |
return "Person{id=" + id + ", name='" + name + '\'' + '}'; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | foo1 | |
---|---|---|
2 | foo2 | |
3 | Bessat J-D & Graf J-M | |
4 | foo4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<persons> | |
<person> | |
<id>1</id> | |
<name>foo1</name> | |
</person> | |
<person> | |
<id>2</id> | |
<name>foo2</name> | |
</person> | |
<person> | |
<id>3</id> | |
<name>Bessat J-D & Graf J-M</name> | |
</person> | |
<person> | |
<id>4</id> | |
<name>foo4</name> | |
</person> | |
</persons> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment