Skip to Content
Personal Insights
Author's profile photo Nitin Pandey

Removing any xml node from the given payload

Hellow folks,

Question.- There was a requirement that we had to remove any given node dynamically from the given payload.

The given payload was this –

Here our payload was encoded and we had our payload in json format.

So our question was to remove all the tags present in excludeXMLTags separated by | from the given encoded content.

 

Solution – 

 

  • So here first of all we have used start timer and content modifier to store the given json payload.

 

  • After this we have used Json to XML converter to convert the payload from Json to XML.

 

  • After this we have used a content modifier where we have stored our
    • content and attchments in header.
    • and excludeXMLTags in property.
    • and at last we have called the content which we have to decode.here we have saved the body of content as decoder.

 

  • After this we have used Base64Decoder for decoding the encoded content.

 

  • And at last, we have used Groovy Script where we first of all we splitted the excludeXMLTags on the basis of | and after this we have removed these nodes from the payload of content.

 

 

  • Code for groovy is shown above and given below:–

 

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.xml.XmlUtil

def Message processData(Message message) {
// Get the header value containing node names to remove
def headerValue = message.getProperty(“excludeXMLTags”)

if (headerValue) {
// Split the header value by ‘|’ to get a list of node names
def nodeNamesToRemove = headerValue.split(“\\|”)

// Convert the message body to a string
def payload = message.getBody(String.class)

// Parse the XML
def xml = new XmlSlurper().parseText(payload)

// Remove the specified tags and their content
nodeNamesToRemove.each { tagNameToRemove ->
xml.’**’.findAll { it.name() == tagNameToRemove }.each { it.replaceNode {} }
}

// Convert the modified XML back to a string
def modifiedPayload = XmlUtil.serialize(xml)

// Set the modified payload as the new message body
message.setBody(modifiedPayload)
}

// Return the modified message
return message
}

 

Thanks

Nitin Pandey  and Bipul Kumar  !

 

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.