on 01-29-2019 10:17 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Hi Vijay,
The target should be XML.
Best Regards,
MANI
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mani,
What is your target xml or xsd ...
Thanks and Regards,
Vijay.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.