on 07-20-2023 4:42 PM
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>
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
81 | |
11 | |
8 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.