2. Today we will be discussing on transformation
of CSV to XML format of data.
The configuration details for each of the Mule flow
component is explained below.
The first component of the flow is File Endpoint.
3. The File endpoint is configured to pick up files from specific
location and on completion of processing move it to a pre-
defined location. The developer is free to choose location
as per his/her convenience.
For our demonstration, in the General tab, configure the
following
Path –
C:UsersuserAnypointStudioworkspacetestcsvtoxmlsrcte
stresourcesinput
Move to Directory – C:MuleStudioFileOutbound g
properties with specified values:
4. Move to Directory –
C:UsersuserAnypointStudioworkspacetestcsvtoxmlsrcte
stresourcesoutput
The next component is logger. The message property value
of the logger component defines the logging statement. In
the General tab, configure the following.
Message – #[string: output #[message]]
The message object provides developer with insights
regarding message’s meta-data.
5. The contents of the file read by the File endpoint
component is passed to the CSV to Maps component for
conversion to Map data structure. Each line in the CSV file is
converted into an individual Map instance. The individual
Map instances are added to a Collection and passed on to
the next message processor in the flow chain. The
component uses FlatPack project to do file parsing. The
parsed file definition is provided via the mapping file
property. The file needs to be present in the application
classpath. To ensure availability, the person.xml is added in
the src/main/java folder.
6. In the General tab of the CSV to Maps component configure
the following properties with specific values
Delimiter – ,
Mapping file – person.xml
7. <?xml version='1.0'?>
<!-- DTD can be pulled from the Jar or over the web-->
<!DOCTYPE PZMAP SYSTEM "flatpack.dtd" >
<!--<!DOCTYPE PZMAP SYSTEM
"http://flatpack.sourceforge.net/flatpack.dtd" >-->
<PZMAP>
<COLUMN name="FIRSTNAME" />
<COLUMN name="LASTNAME" />
8. <COLUMN name="DOB" />
</PZMAP>
In the Java component’s General tab configure the
following property with specific value.
Class Name – com.vinraj.integration.file.CSVProcessor
The source code of the CSVProcessor is shown below.
9. package testcsvtoxml;
import java.util.List;
import java.util.Map;
public class CSVProcessor {
public void processFile(List<Map<String, String>> maps) {
System.out.println(maps);
System.out.println("Size: " + maps.size());
}
}
10. For testin we can use into csv
John,Kent,11/02/1972
Jane,Tully,1/12/1978
Robert,Smith,1/12/1988