Monday, 21 January 2019

Remove XML node in XML using groovy script

Groovy script for removing the xml tag from the XML.

Below code is working:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.util.*;

def Message processData(Message message) {
    //Body
       //def body = message.getBody();
       def body = message.getBody(java.lang.String) as String;
        def xml = new XmlSlurper().parseText(body)
 xml.children().findAll { it.name() == 'CustomerID' }.replaceNode {}
   
   body = XmlUtil.serialize(xml)
   message.setBody(body);
       return message;
}

Input:
<?xml version="1.0" encoding="UTF-8"?>
<IndividualCustomerCollection>
        <IndividualCustomer>
                               <CustomerID>343565</CustomerID>
                               <RoleCode>CRM000</RoleCode>
                               <TitleCode>0001</TitleCode>
        </IndividualCustomer>
</IndividualCustomerCollection>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<IndividualCustomerCollection>
        <IndividualCustomer>
                               <RoleCode>CRM000</RoleCode>
                               <TitleCode>0001</TitleCode>
        </IndividualCustomer>
</IndividualCustomerCollection>



2 comments:

  1. Copy pasted the codes and input message but I don't see any CustomerID removal. Can you help me if I'm doing something wrong. Thanks.

    ReplyDelete
  2. Sorry for the delay. I am using this code, it is working for my scenario

    ReplyDelete