Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 


This blog helps you solve most common requirement to convert nested CSV structure to an XML in SAP CPI.

Many times, we are required to convert CSV structures to deeply nested XML structures. Standard Conversion methods in SAP CPI allow you to convert the incoming CSV file to flat XML structures only i.e., only one level of nesting is possible.

IF we are receiving a complex/nested csv files from third party system, then standard CSV converters in SAP CPI would not help us. In that case we need to use of either Groovy Script or third-party conversion agents or inbuilt graphical mapping by using intermediate structures and user defend functions.

This blog explains about the Groovy script used to convert a CSV structure to Nested XML file.  In the below scenario, I have taken a sample XML file and converted that to an output XML file using Groovy script in SAP CPI.


Current Scenario:

In this scenario I get a Purchase Order and multiple Line Items associated to it. Received multiple purchase Orders from the client as shown in Input File. We need to convert this .csv file into an XML File as shown in the output File.

Below groovy script can be used to achieve this requirement.

Sample Input CSV File :


Output XML:


In the CSV_XML Converter, i have added the Groovy script mentioned below.


Groovy Script:

Code Explanation:

  • getRows Method splits each line and store it in a array(lines). Lines array is split based on the commas and stored in List(rows).

  • getListNodes creates an array of objects with each PO and associated Items together.

  • generateXML Method converts the object to the XML format. I have hardcoded the fields for code simplicity. You can enhance the code based on your requirement.

import groovy.xml.*;

def getRows(body) {
String[] lines = body.split("\\r?\\n")
lines[0].replace('\n', '')
// List<String[]> rows = lines.collect {it.tokenize(',')}
List<String[]> rows = lines.collect {it.split(',')}
println rows
return rows

def getListNodes(input) {
def listOfNodes = [];
for (int i = 0; i < input.size(); i++) {
//Identifier for the Purchase Order is “PO”
if (input[i][0] == 'PO') {
def obj = [
PurchaseOrders: input[i],
items : []

for (int j = i + 1; j < input.size(); j++) {
//All the “ITEM” will come under the “PO” structure.
if (input[j][0] != 'PO') {
} else {
i = j - 1;

return listOfNodes

def generateXML(xml, listNodes) {
// println listNodes
listNodes.each{it ->
def currHeader = it.get('PurchaseOrders')
def currNode = it.get('items')

xml.PurchaseOrders {
currNode.each { it1 ->

Items {

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

def rows = getRows(body)
def listNodes = getListNodes(rows)
generateXML(xml, listNodes)

return message



So, to conclude, this blog post helps you in converting a complex .csv into an XML in SAP CPI in cases where standard converter is not sufficient.

If you have any queries, please feel free to ask your question in comments. I would request everyone to provide your feedback and like if this blog post finds helpful for you.


Thanks & Regards,

Kavya Priya
Labels in this area