escape any xml specail char in xml tag in xml payload , using regular expression groovy code.

 xml escape in payload content.


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

import java.util.regex.Matcher;

import java.util.regex.Pattern;


def Message processData(Message message) {

    def body = message.getBody(String)

    

    // Regular expression to match XML tags with or without content

    def pattern = ~/<([^>]+)>(.*?)<\/\1>|<([^>]+)\/>/  // Adjusted to capture both self-closing and regular tags

    def matcher = pattern.matcher(body)

    

    def result = body // Initialize result with the original body in case no match is found

    

    // Process matching tags

    while (matcher.find()) {

        def fullMatch = matcher.group(0)      // Full matched XML tag (including tag name and content)

        def tagName = matcher.group(1)        // Tag name (for regular tags)

        def content = matcher.group(2)        // Content inside the tag

        def selfClosingTag = matcher.group(3) // Self-closing tag (like <tag />)


        if (selfClosingTag) {

            // If it's a self-closing tag, simply add it back to the result

            result = result.replace(selfClosingTag, "<${selfClosingTag}/>") 

        } else if (tagName) {

            // Escape special characters in the content

            content = content.replaceAll("&", "&amp;")

                             .replaceAll("<", "&lt;")

                             .replaceAll(">", "&gt;")

                             .replaceAll("\"", "&quot;")

                             .replaceAll("\'", "&apos;")

                            // .replaceAll("", "&nbsp;") // Escape spaces with non-breaking space

            

            // Rebuild the XML tag with escaped content

            def escapedTag = "<${tagName}>${content}</${tagName}>"

            // Replace the original tag with the escaped version in the result

            result = result.replace(fullMatch, escapedTag)

        }

    }

    

    message.setBody(result)

    return message;

}

----

other recomded by groovy;

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

import groovy.xml.*;

import java.io.StringWriter;

import java.io.PrintWriter;


def Message processData(Message message) {

    def body = message.getBody(String)

    

    def parser = new XmlParser()

    def root = parser.parseText(body)

    

    def escapeTextContent(node) {

        if (node instanceof Node) {

            node.value().eachWithIndex { child, index ->

                if (child instanceof String) {

                    // Escape special characters in text content

                    def content = child.toString()

                    content = content.replaceAll("&", "&amp;")

                                     .replaceAll("<", "&lt;")

                                     .replaceAll(">", "&gt;")

                                     .replaceAll("\"", "&quot;")

                                     .replaceAll("\'", "&apos;")

                                     .replaceAll(" ", "&nbsp;")

                    // Replace the text content with the escaped content

                    node.value()[index] = content

                } else if (child instanceof Node) {

                    // Recursively process child nodes

                    escapeTextContent(child)

                }

            }

        }

    }

    

    escapeTextContent(root)

    

    // Convert the modified XML back to string

    def writer = new StringWriter()

    def xmlPrinter = new XmlNodePrinter(new PrintWriter(writer))

    xmlPrinter.preserveWhitespace = true

    xmlPrinter.print(root)

    def result = writer.toString()

    

    message.setBody(result)

    return message;

}


Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024