<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial, we will discuss about an application where we see how to configure a Spring Batch job to read XML file by JAXB2 library into a csv file, and filter out the record before writing with <i><b>ItemProcessor</b></i>.</p>
<h3><b>Tools and libraries used</b></h3>
<ul style="text-align: left;">
<li>Spring Tool Suite (STS)</li>
<li>DK 1.6</li>
<li>Spring Core 3.2.2.RELEASE</li>
<li>Spring OXM 3.2.2.RELEASE</li>
<li>Spring Batch 2.2.0.RELEASE</li>
</ul>
<div id="ads-id" align="center"></div>
<h2><strong>Sample Example of Spring Batch XML To CSV-</strong></h2>
<h3><b>1. Project Directory Structure</b></h3>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/03/111-7.png" border="0" /></div>
<h3><strong>2. Input XML File</strong><br />
<b>employees.xml</b></h3>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;employees>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;17<;/age>; 
 <;empid>;1111<;/empid>; 
 <;name>;ATUL KUMAR<;/name>; 
 <;salary>;300000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;27<;/age>; 
 <;empid>;2222<;/empid>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;60000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;21<;/age>; 
 <;empid>;3333<;/empid>; 
 <;name>;ASHUTOSH RAJPUT<;/name>; 
 <;salary>;400000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;Kanpur<;/address>; 
 <;age>;27<;/age>; 
 <;empid>;4444<;/empid>; 
 <;name>;Adesh Verma<;/name>; 
 <;salary>;80000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;Noida<;/address>; 
 <;age>;37<;/age>; 
 <;empid>;5555<;/empid>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;300000.0<;/salary>; 
 <;/employee>; 
<;/employees>; 
</pre>
<h3><strong>3. <i>ItemReader </i>for XML File</strong></h3>
<p>In this example, we use <b><i>Jaxb2Marshaller </i></b>to map XML values and attributes to an object.</p>
<pre class="highlight"><;bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">; 
 <;property name="resource" value="classpath:xml/employees.xml" />; 
 <;property name="unmarshaller" ref="empUnMarshaller" />; 
 <;property name="fragmentRootElementName" value="employee" />; 
 <;/bean>; 
</pre>
<p><b>Employee.java</b></p>
<pre class="highlight">package com.doj.batch.bean; 
 
import javax.xml.bind.annotation.XmlAccessOrder; 
import javax.xml.bind.annotation.XmlAccessorOrder; 
import javax.xml.bind.annotation.XmlRootElement; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@XmlRootElement(name="employee") 
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED) 
public class Employee { 
 private int empid; 
 private String name; 
 private int age; 
 private float salary; 
 private String address; 
 /** 
 * @return the empid 
 */ 
 public int getEmpid() { 
 return empid; 
 } 
 /** 
 * @param empid the empid to set 
 */ 
 public void setEmpid(int empid) { 
 this.empid = empid; 
 } 
 /** 
 * @return the name 
 */ 
 public String getName() { 
 return name; 
 } 
 /** 
 * @param name the name to set 
 */ 
 public void setName(String name) { 
 this.name = name; 
 } 
 /** 
 * @return the age 
 */ 
 public int getAge() { 
 return age; 
 } 
 /** 
 * @param age the age to set 
 */ 
 public void setAge(int age) { 
 this.age = age; 
 } 
 /** 
 * @return the salary 
 */ 
 public float getSalary() { 
 return salary; 
 } 
 /** 
 * @param salary the salary to set 
 */ 
 public void setSalary(float salary) { 
 this.salary = salary; 
 } 
 /** 
 * @return the address 
 */ 
 public String getAddress() { 
 return address; 
 } 
 /** 
 * @param address the address to set 
 */ 
 public void setAddress(String address) { 
 this.address = address; 
 } 
 
} 
</pre>
<p>In JAXB2, for complex data type like Date , will not map to the field automatically, even it’s annotated.</p>
<p>for this to make JAXB2 supports Date conversion, you need to create a custom Adapter to handle the Date format manually, then attaches the adapter via <b><i>@XmlJavaTypeAdapter. </i></b>Lets see how to map.</p>
<pre class="highlight">package com.doj.batch.adapter; 
 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class DataJaxbAdapter extends XmlAdapter<;String, Date>;{ 
 
 private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
 
 @Override 
 public Date unmarshal(String date) throws Exception { 
 return dateFormat.parse(date); 
 } 
 
 @Override 
 public String marshal(Date date) throws Exception { 
 return dateFormat.format(date); 
 } 
 
} 
</pre>
<p> ;</p>
<pre class="highlight">@XmlRootElement(name="employee") 
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED) 
public class Employee { 
... 
... 
private Date doj; 
... 
@XmlJavaTypeAdapter(JaxbDateAdapter.class) 
 @XmlElement 
 public Date getDoj() { 
 return doj; 
 } 
... 
... 
 
} 
</pre>
<h3><strong>4. Spring Batch Core configuration</strong></h3>
<p>Define jobRepository and jobLauncher.<br />
<b>applicationContext.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">; 
 
 <;bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>; 
 
 <;bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">; 
 <;property name="jobRepository" ref="jobRepository"/>; 
 <;/bean>; 
 
 <;bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">; 
 <;property name="transactionManager" ref="transactionManager"/>; 
 <;/bean>; 
 
 <;bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob" abstract="true">; 
 <;property name="jobRepository" ref="jobRepository" />; 
 <;/bean>; 
 
<;/beans>; 
</pre>
<h3>5. Spring Batch Jobs Configuration file</h3>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:batch="http://www.springframework.org/schema/batch" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd">; 
 
 <;import resource="applicationContext.xml"/>; 
 
 <;bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">; 
 <;property name="resource" value="classpath:xml/employees.xml" />; 
 <;property name="unmarshaller" ref="empUnMarshaller" />; 
 <;property name="fragmentRootElementName" value="employee" />; 
 <;/bean>; 
 
 <;bean id="filterDataProcessor" class="com.doj.batch.processor.DataFilterProcessor" />; 
 
 <;bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">; 
 <;property name="shouldDeleteIfExists" value="true" />; 
 <;property name="resource" value="file:csv/outputs/employees.csv" />; 
 <;property name="lineAggregator">; 
 <;bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">; 
 <;property name="delimiter" value="," />; 
 <;property name="fieldExtractor">; 
 <;bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">; 
 <;property name="names" value="empid, name, age, salary, address" />; 
 <;/bean>; 
 <;/property>; 
 <;/bean>; 
 <;/property>; 
 <;/bean>; 
 
 <;bean id="empUnMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">; 
 <;property name="classesToBeBound">; 
 <;value>;com.doj.batch.bean.Employee<;/value>; 
 <;/property>; 
 <;/bean>; 
 
 <;batch:job id="simpleDojJob" job-repository="jobRepository" parent="simpleJob">; 
 <;batch:step id="step1">; 
 <;batch:tasklet transaction-manager="transactionManager">; 
 <;batch:chunk reader="xmlItemReader" processor="filterDataProcessor" writer="csvItemWriter" commit-interval="1"/>; 
 <;/batch:tasklet>; 
 <;/batch:step>; 
 <;/batch:job>; 
<;/beans>; 
</pre>
<h3><strong>6. Spring Batch ItemProcessor for filter data</strong></h3>
<p>In Spring batch, the wired Processor will be fired before writing to any resources, so, this is the best place to handle any conversion, filtering and business logic. In this example, we will be ignored all employees whose have salaried less than 70000 i.e. not write to csv file.<br />
<b>DataFilterProcessor.java</b></p>
<pre class="highlight">package com.doj.batch.processor; 
 
import org.springframework.batch.item.ItemProcessor; 
 
import com.doj.batch.bean.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class DataFilterProcessor implements ItemProcessor<;Employee, Employee>; { 
 
 @Override 
 public Employee process(Employee emp) throws Exception { 
 if(emp.getSalary() >; 70000.0){ 
 return emp; 
 }else{ 
 return null; 
 } 
 } 
 
} 
 
</pre>
<h3><strong>7. Launching Batch Job-</strong></h3>
<p>Spring Batch comes with a simple utility class called CommandLineJobRunner which has a main() method which accepts two arguments. First argument is the spring application context file containing job definition and the second is the name of the job to be executed.</p>
<p>Now run as a java application with both two arguments.<br />
<i><b>org.springframework.batch.core.launch.support.CommandLineJobRunner</b></i><br />
<i><b>simple-job.xml simpleDojJob</b></i></p>
<p>Output. Extracts all employees into an csv file from xml file.<br />
<b>employees.csv</b></p>
<p><i>1111,ATUL KUMAR,17,300000.0,delhi</i><br />
<i>3333,ASHUTOSH RAJPUT,21,400000.0,delhi</i><br />
<i>4444,Adesh Verma,27,80000.0,Kanpur</i><br />
<i>5555,Dinesh Rajput,37,300000.0,Noida</i></p>
<h3><b>Download Source Code with libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/SpringBatchXMTtoCSV.zip?attredirects=0&;d=1" target="_blank" rel="noopener">SpringBatchXMTtoCSV.zip</a></b></h3>
<p> ;</p>
</div>
<p> ;</p>
<div style="text-align: center;"><b><;<;<a href="https://dineshonjava.com/spring-batch-mysql-database-to-xml/">Previuos</a><;<; <a href="https://dineshonjava.com/spring-tutorial/">Index </a>>;>; <a href="https://dineshonjava.com/spring-batch-csv-to-mysql-database/">Next</a>>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-batch-mysql-database-to-xml/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-batch-csv-to-mysql-database/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '224'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…