cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script for removing reserved character inside XML node

jbesl
Explorer
0 Kudos
831

Dear CPI experts. 

Good day, 

Looking to get assistance with my requirement below. 

The idea is to remove all below reserved characters from inside the all XML nodes if they are present.

However below script im trying to work with, but does not work. It is not preserving the XML structure hence the entire output is incorrect.

reserved characters *%?:;=()[]|#@

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

def Message processData(Message message) {
    def body = message.getBody(String) 

    def root = new XmlSlurper().parseText(body)

    def cleanNodeContent
    cleanNodeContent = { node ->
        if (node instanceof groovy.util.slurpersupport.NodeChild) {
            if (node.text()) {
                // Remove reserved characters: *%?:;=()[]|#@
                def cleanedText = node.text().replaceAll('[*%?:;=()\\[\\]\\|#@]', '')
                node.setValue(cleanedText)
            }
        }

        node.children().each { child ->
            cleanNodeContent(child)
        }
    }

    cleanNodeContent(root)

    def cleanedXml = XmlUtil.serialize(root)

    message.setBody(cleanedXml) // Set the modified body back to the message
    return message
}

 #

Accepted Solutions (1)

Accepted Solutions (1)

nageshrao
Product and Topic Expert
Product and Topic Expert
0 Kudos

Example XML body with reserved characters - 

<
employees>

    <employee>
        <id>EMP123</id>
        <name>John *Doe%</name>
        <email>john.doe@:company.com</email>
        <department>Sales & Marketing</department>
        <role>Manager#</role>
        <address>1234 Elm Street; Apt [5B]</address>
        <phone>(555) 123-4567</phone>
        <notes>Key account handler | VIP clients</notes>
    </employee>
</employees>


Groovy script to remove all reserved characters and keep the XML structure intact.

 

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.regex.Pattern

def Message processData(Message message) {
    def body = message.getBody(String)
    
    // Define reserved characters and pattern
    def reservedChars = ['*', '%', '?', ':', ';', '=', '(', ')', '[', ']', '|', '#', '@']
    def pattern = reservedChars.collect { Pattern.quote(it) }.join('|')

    // Function to clean reserved characters from XML content
    def cleanContent = { content ->
        return content.replaceAll(pattern, '')
    }

    // Replace reserved characters in XML content manually
    def cleanedBody = body.replaceAll(/(<[^>]+>)([^<]*)(<\/[^>]+>)/) { match ->
        def startTag = match[1]
        def content = match[2]
        def endTag = match[3]
        return startTag + cleanContent(content) + endTag
    }

    // Set cleaned XML body
    message.setBody(cleanedBody)
    return message
}

 

 

jbesl
Explorer
0 Kudos
Thank you. Is there a list of allowed Groovy Import / Java libraries for SAP CPI somewhere ?

Answers (0)