cancel
Showing results for 
Search instead for 
Did you mean: 

XML to CSV Conversion with each value enclosed in double quotes

0 Kudos

Hi Experts,
When converting the Xml Structure to CSV , I am getting the output as below 

000123678|john|jerry|dummy@gmail.com|1234567584
0001236012|lusy|lams|dummy2@gmail.com|3847667584


But i need the output in the below mentioned format each value should be enclosed in double quotes

"000123678"|"john"|"jerry"|"dummy@gmail.com"|"1234567584"
"0001236012"|"lusy"|"lams"|"dummy2@gmail.com"|"3847667584"
Can someone help me with this ,below is the sample input xml payload

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
   <Record>
      <EmployeeID>000123678</EmployeeID>
      <FirstName>john</FirstName>
      <LastName>jerry</LastName>
      <Email>dummy@gmail.com</Email>
      <Phone>1234567584</Phone>
   </Record>
   <Record>
      <EmployeeID>0001236012</EmployeeID>
      <FirstName>lusy</FirstName>
      <LastName>lams</LastName>
      <Email>dummy2@gmail.com</Email>
      <Phone>3847667584</Phone>
   </Record>
</Employees>

 XML to CSV Converter Configurations

pavankalyan_racha_0-1716374989748.png

Thanks,
Pavan Kalyan Racha

View Entire Topic
snayak
Explorer
0 Kudos

Hi @pavankalyan_racha you can achieve this output by using a groovy script. Add the below groovy script after your XML to CSV converter step:-

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

def Message processData(Message message) {
    // retrieve the CSV content from the message
    def csvContent = message.getBody(java.lang.String) as String
    
    // split the CSV content into lines
    def lines = csvContent.split('\n')
    
    // process each line
    def modifiedCsv = lines.collect { line ->
        // split the line into fields using '|'
        def fields = line.trim().split('\\|')
        
        // wrap each field in double quotes
        def quotedFields = fields.collect { field ->
            "\"${field}\""
        }
        
        // join the quoted fields with '|'
        quotedFields.join('|')
    }.join('\n')     
   
    message.setBody(modifiedCsv)
    
    // Return the modified message
    return message
}

Regards,

Sailee