This script example is contributed by Suraj Shelke.
It illustrates how you can work with splitting, arrays, and loops in Groovy.
Since there might be many edge cases with CSV, you should also consider using a library like Apache Commons CSV.
This script example is contributed by Suraj Shelke.
It illustrates how you can work with splitting, arrays, and loops in Groovy.
Since there might be many edge cases with CSV, you should also consider using a library like Apache Commons CSV.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil
def Message processData(Message message) {
//Body
def body = message.getBody(String);
def lines = body.trim().split('\n')
def headers = lines[0].split(',')
def data = lines[1..-1].collect { it.split(',') }
def xml = new StringBuilder()
xml.append('<root>')
data.each {
row ->
xml.append('<record>')
headers.eachWithIndex {
header,
index ->
xml.append("<${header}>${row[index]}</${header}>")
}
xml.append('</record>')
}
xml.append('</root>')
def formattedXml = XmlUtil.serialize(xml.toString())
message.setBody(formattedXml)
return message;
}HEADER1,B,C
1,2,3
4,5,6<?xml version="1.0" encoding="UTF-8"?>
<root>
<record>
<HEADER1>1</HEADER1>
<B>2</B>
<C>3
</C>
</record>
<record>
<HEADER1>4</HEADER1>
<B>5</B>
<C>6</C>
</record>
</root>