Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
BhaskarY
Participant
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.
2 Comments
Labels in this area