Tuesday 30 July 2024

Multiple header and multiple item xml.to csv conversion

 CSV file to be prepared with multiple header with multiple line item on it

 

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()

}

 

 

}




No comments:

Post a Comment