Tuesday 8 August 2023

CSV file to be prepared with multiple header with multiple line item using groovy script


On receiver side CSV file generation, CSV file to be prepared with multiple header with multiple line item using groovy script:

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

import groovy.xml.*;


def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;

def ag = new xml2csv()

message.setBody(ag.execute(body))


return message

}



class xml2csv {

def execute(ins) {

//define writer

def writer = new StringWriter()

//parse input

def parsedXml = new XmlParser().parseText(ins)

def content = new XmlSlurper().parseText(ins)

def header = content.Header.children().collect().join(',')

def csv = content.Item.inject(header){ result, row ->

[result, row.children().collect().join(',')].join("\n")

}

println csv.toString()

return csv.toString()

}

}








1 comment:

  1. Better if you can provide input and output data as well in blogs, it will be easy to understand the content.

    ReplyDelete