cancel
Showing results for 
Search instead for 
Did you mean: 

How To remove nodes and xml declaration by using a single groovy script?

sayed42
Explorer

I want to remove mapping tags, example (<multimap:Message1> , <ns0:Messages>, etc) also the xml Declaration, example(<?xml version='1.0' encoding='UTF-8'?>)

I know I can remove this all by using XML Modifier and Filter pallet functions but I want to try it with a single groovy script. Please let me know if you find any solution. Thanks & regards,

Sufiyan Sayed

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> 
                <EmpData> 
                   <Employee> 
                      <Name>Sufiyan</Name> 
                      <empID>101</empID> 
                      <grade>A</grade> 
                      <Role>Software Engineer</Role> 
                      <Dept> 
                          <Name>Sales</Name> 
                          <deptID>13345</deptID> 
                          <grade>A</grade> </Dept> 
                   </Employee> 
                </EmpData> 
              </ns0:Message1> 
           </ns0:Messages
        ></multimap:Message1>
    </multimap:Messages> 
  

Expected Output:

 <EmpData> 
    <Employee> 
       <Name>Sufiyan</Name> 
       <empID>101</empID> 
       <grade>A</grade> 
       <Role>Software Engineer</Role> 
       <Dept> 
       <Name>Sales</Name> 
       <deptID>13345</deptID> 
       <grade>A</grade> 
       </Dept> 
    </Employee> 
</EmpData>
MortenWittrock
Active Contributor

Hi Sufiyan

Please use the CODE button for your sample payloads. Without it, the blogging platform messes up the XML content badly.

Regards,

Morten

sayed42
Explorer
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a

I've updated the question, Please check and let me know.

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

sayed42
Explorer

Hi experts,

I just got the solution by trying from myside, which is quite hilarious,

I got the desired output but Still if we can make this code more durable or in less time complexity then let me know.

Thanks,

Sufiyan.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
    def removal=message.getBody(java.lang.String) as String;
    removal=removal.replace('''<?xml version='1.0' encoding='UTF-8'?>''',"");
    removal=removal.replaceAll('''<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">''',"");
    removal=removal.replaceAll("<multimap:Message1>","");
    removal=removal.replaceAll('''<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">''',"");
    removal=removal.replaceAll("<ns0:Message1>","");
    removal=removal.replaceAll("</ns0:Message1>","");
    removal=removal.replaceAll("</ns0:Messages>","");
    removal=removal.replaceAll("</multimap:Message1>","");
    removal=removal.replaceAll("</multimap:Messages>","");
    
    
    message.setBody(removal);
    return message;
} 
MortenWittrock
Active Contributor

Hi Sufiyan

Please don't use string handling to transform an XML document. It's very fragile. If a namespace prefix changes, the code breaks. If whitespace changes, the code breaks.

Any script that solves your problem will be more complicated than a single Filter step, which is all it takes to solve it using the built-in flow steps. So please do that instead 😄

If your reason for asking is that you want to learn about XML processing in Groovy, this page is a good place to start.

Regards,

Morten

sayed42
Explorer
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a ,

Thanks for the answer I'll note your point.

Answers (0)