If we need a change an xml message with specific condition of an existing error, in an example of error response seeing below, we can use Groovy with XmlSlurper.
Sample Message:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>
soap:Server
</faultcode>
<faultstring>
javax.xml.ws.WebServiceException: com.exception.BaseUserException: Some Error.
</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
XML
Groovy Expression:
import groovy.xml.XmlSlurper
import groovy.xml.XmlUtil
var rootNode = new XmlSlurper(false, true).parseText(responseBodyTextFromTargetAPI).declareNamespace('soap':'http://schemas.xmlsoap.org/soap/envelope/')
def element=rootNode.'soap:Body'.'soap:Fault'.'faultstring'
if(element != ''){
rootNode.'soap:Body'.'soap:Fault'.replaceNode { }
rootNode.'soap:Body'.appendNode {
faultstring(element.text())
}
responseBodyTextFromTargetAPI = XmlUtil.serialize(rootNode)
}
GROOVY
Result:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<faultstring>
javax.xml.ws.WebServiceException: com.exception.BaseUserException: Some Error.
</faultstring>
</soap:Body>
</soap:Envelope>
XML