Skip to Content
Technical Articles
Author's profile photo Bhaskar Yerramilli

Remove XML Declarations and Unwanted Message nodes from XML using groovy script in CPI

For the scenarios that includes XML declarations and the unwanted tags <ns0:Message>,<ns0:Message1>, <multimap:Messages> and <multimap:Message1>, we generally use the XML modifier, XSLT mapping and other pallet functions to remove them. But to remove these unwanted tags and declarations directly using a groovy script, you can use the below script.

Script:

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

def Message processData(Message message) {
    def inputXml = message.getBody(String)
    def outputXml = extractFirstChild(inputXml)
    message.setBody(outputXml)
    return message
}

def extractFirstChild(String inputXml) {
    def matcher = inputXml =~ /<ns0:Message1[^>]*>((?:.|[\r\n])*?)<\/ns0:Message1>/
    if (matcher) {
        return matcher[0][1].trim()
    } else {
        return inputXml
    }
}

Input:

 <?xml version='1.0' encoding='UTF-8'?>
    <multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
       <multimap:Message1>
         <ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
             <ns0:Message1> 
                <root> 
                   <Parent> 
                      <Child1>Value1</Child1> 
                      <Child2>Value2</Child2> 
                      <Child3>Value3</Child3> 
                      <Child4> 
                          <field1>Value4</field1> 
                          <field2>Value5</field2>
                      </Child4> 
                   </Parent> 
                </root> 
              </ns0:Message1> 
           </ns0:Messages
        ></multimap:Message1>
    </multimap:Messages> 

Output:

<root> 
   <Parent> 
       <Child1>Value1</Child1> 
       <Child2>Value2</Child2> 
       <Child3>Value3</Child3> 
       <Child4> 
           <field1>Value4</field1> 
           <field2>Value5</field2>
       </Child4> 
   </Parent> 
</root>

 

I hope, you will be benefitted from the above script.

Please feedback or comment below, if you find any other way of scripting to remove the tags.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Morten Wittrock
      Morten Wittrock

      Hi Bhaskar

      My general advice is to not do something in a script, if there is a non-script solution (i.e. using the built-in steps) of comparable or lower complexity. In this specific case, the non-script solution (a single Filter step) is much, much less complicated.

      Furthermore, processing XML with regular expressions is in general also a pretty bad idea. The code is vulnerable to subtle changes in the XML document, that would not impact the Filter step.

      This makes the non-script solution not only less complicated, but also more robust. So for this particular problem, I strongly recommend that you use the Filter step instead.

      Kind regards,

      Morten

      Author's profile photo Bhaskar Yerramilli
      Bhaskar Yerramilli
      Blog Post Author

      Hi Morten,

      Thanks a lot for the kind suggestion.

      Regards,

      Bhaskar