Wednesday 5 June 2019

Converting the File from field fixed length file to XML Success-factor Simplified


There is no standard way of converting the flat file into XML format using the fixed field length in CPI, as we have in PI using FCC parameters. To solve this problem, you can implement the below code.


GroovyScript:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

    def Message processData(Message message) {

//Body

    def body = message.getBody(java.lang.String) as String;
    def lines = body.split("\n");

//root node
    def body1 ="<Files>";

//looping to read the lines and form a XML
    for(i=0;i<lines.length;i++)

     {

         body1 += "<EmpDetails>";
         body1 += "<Name>"+lines[i].substring(0,4)+"</Name>";
         body1 += "<Age>"+lines[i].substring(4,6)+"</Age>";
         body1 += "<DOB>"+lines[i].substring(6,26)+"</DOB>";
         body1 += "<JobTitle>"+lines[i].substring(14,26)+"</JobTitle>";
         body1 += "<Country>"+lines[i].substring(26,31)+"</Country>";
         body1 += "</EmpDetails>";
    }

//closing root note
    body1 +="</Files>";

//storing the converted XML to body
    message.setBody(body1);

    return message;

   }


Input File(Flat):

Rama2123051990SrConsultantIndia
Ravi3322021994 ConsultantIndia
Siva3322021994SrConsultantIndia

Output File(XML):

<Files>
<EmpDetails>
<Name>Rama</Name>
<Age>21</Age>
<DOB>23051990SrConsultant</DOB>
<JobTitle>SrConsultant</JobTitle>
<Country>India</Country>
</EmpDetails>
<EmpDetails>
<Name>Ravi</Name>
<Age>33</Age>
<DOB>22021994 Consultant</DOB>
<JobTitle> Consultant</JobTitle>
<Country>India</Country>
</EmpDetails>
<EmpDetails>
<Name>Siva</Name>
<Age>33</Age>
<DOB>22021994SrConsultant</DOB>
<JobTitle>SrConsultant</JobTitle>
<Country>India</Country>
</EmpDetails>
</Files>

Hope this helps!!!





1 comment:

  1. Hi, If the input file has two different fixed length types of data then is there any method for conversion to xml?

    ReplyDelete