cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Groovy script remove empty lines in body

0 Likes
4,872

hi,

I have used xml to csv converter in iflow and I got the below content as message body

UTF-8


DepartmentID,Parent
101040,10423
1010120,10340

I have to remove 2 empty lines after UTF-8.I have used below script

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def body = message.getBody(String.class);
body = body.replaceAll("\r\n","");
message.setBody(body);
return message;
}

I am getting out put as below as one string

UTF-8DepartmentID,Parent101040,104231010120,10340

I want to have the content as separate lines like above but without any blank lines. How do I achieve this

Accepted Solutions (0)

Answers (2)

Answers (2)

vadimklimov
Active Contributor

Hello Sankara,

You can use the following Groovy script function to remove empty lines:

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

Message processData(Message message) {
    Reader input = message.getBody(Reader)
    Writer output = new StringWriter()
    input.filterLine(output) { !it.empty }
    message.setBody(output.toString())
    return message
}

Regards,

Vadim

0 Likes

hi,

I tried this code in groovy ide(https://groovyide.com/cpi), but i am getting below errors

***Error at line: 6***org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.runtime.IOGroovyMethods at com.groovyide.ExecutorService$1.run(ExecutorService.java:144)Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.runtime.IOGroovyMethods at org.codehaus.groovy.runtime.dgm$877.invoke(Unknown Source) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:244) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135) at groovyide_com.processData(groovyide_com:6) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:821) at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44) at groovy.lang.Script.invokeMethod(Script.java:97) ... 1 moreError:

TypeError: Cannot convert undefined or null to object

vadimklimov
Active Contributor
0 Likes

Hello Sankara,

Please, try it in an iFlow in a CPI tenant.

Regards,

Vadim

0 Likes

try using foreach readlines.
if string length is 0 then use replace.

0 Likes

hi,

thanks for the suggestion. can you paste the same code.