cancel
Showing results for 
Search instead for 
Did you mean: 

fixed filed length into XML using groovy in SAP CPI

0 Kudos

Hi CPI Experts,

We have a requirement where we need to convert the flat file ( fixed filed length ) into XML. I saw few blogs where we can achieve this by adding a header to the flat file and then splitting it. But that making iflow bit complex.

It would be better to perform groovy script and achieve the scenario. Can you please share the groovy syntax/code to perform this.

Sample file : field length 8,4,24,15,1,40,4,15

sample.txt

Accepted Solutions (0)

Answers (4)

Answers (4)

0 Kudos

Hey Mani reddy,

May be you can use the below, i was a bit lazy to construct the code for all so i have kept it a bit simple

import java.io.File
import groovy.xml.MarkupBuilder
class readLineByLine {
static main(args) {

def varStringWriter = new StringWriter();
def varXMLBuilder   = new MarkupBuilder(varStringWriter);

String lineItems = "20171118AAAB0009C707000001111       +1234567.11    OSuser - ABC Primary                      00  +7654321.22"+'\n'+
'20161118AAAB0009C707000001111       +1234567.11    )Suser - ABC Primary                      00  +7654321.22  ' + '\n'+
"20151118AAAB0009C707000001111       +1234567.11    OSuser - ABC Primary                      00  +7654321.22      "
String newItem ;
lineItems.eachLine{

line -> newItem = line ;
String newItem1 = newItem.substring(0,29);
String newItem2 = newItem.substring(30,37);
String newItem3 = newItem.substring(37,48);

varXMLBuilder.RECORD{
node1(newItem1);
node2(newItem2);
node3(newItem3);
}
}
def xml = varStringWriter.toString();
xml="<RECORDS>"+xml+"</RECORDS>" ;

println xml ;
}
}

Regards

Akash

0 Kudos

Hi Akash,

Thank you so much.I will definitely try to execute the code and let you know the results.

Regards,

MANI REDDY

0 Kudos

Hi Akash,

I have tried to execute this code in CPI end but am getting the below error:

java.lang.NoSuchMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.readLineByLine() is applicable for argument types: (com.sap.gateway.ip.core.customdev.processor.MessageImpl) values: [com.sap.gateway.ip.core.customdev.processor.

But when i checked in groovy console its is working fine as expected. Do you have any idea on this error ? Please share your thoughts.

Best Regards,

MANI REDDY

0 Kudos

Hey ,

sorry for the delayed response, please do make the groovy script shared has been tweaked as per SAP CPI

Regards

Akash

0 Kudos

Hi Vijay,

The target should be XML.

Best Regards,

MANI

former_member194481
Participant
0 Kudos

Hi Mani ,

what i mean target file is .XSD or .xml file right. You want to convert the flat file to XMl , so it should be .xsd or xml

Regards,

Vijay.

0 Kudos

Hi praveen,

Let me know if you need more information.

former_member194481
Participant
0 Kudos

Hi Mani,

What is your target xml or xsd ...

Thanks and Regards,

Vijay.

0 Kudos

Hi Vijay,

Thanks for the reply. please find the attached sample target file.

BR,

MANI