'No such property for class' using XmlSlurper

SaidAslan November 28, 2016

Hi, 

I have an xml response from web service and need to parse it to set some details of this response (values of ErrCode and ErrMsg elements) to the comments of the issue. All this must be done from the Custom-script postfunction. This is what i've done:

import groovy.xml.*
import groovy.util.XmlSlurper
 
def xml = '''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
	<SMSGateWayResponse xmlns="http://xxxx">
		<SMSGateWayResult xmlns:a="http://xxxx" xmlns:i="xxxxx">
				<a:ErrCode>OK</a:ErrCode>
			<a:ErrMsg>OK</a:ErrMsg>
		</SMSGateWayResult>
	</SMSGateWayResponse>
</s:Body>
</s:Envelope>
'''
 
def xmlresult = new XmlSlurper().parseText(xml)
 
//want to do something like this
def res = xmlresult.Body.SMSGateWayResponse.SMSGateWayResult.ErrMsg.text()

and this res result is added to the comment

commentManager.create(
        issue,
        user,
        "response: $res",
        false)

But console returns me error No such property: SMSGateWayResponse for class: groovy.util.slurpersupport.GPathResult.

This is not working, too. 

Please help! 

1 answer

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2016

Do you need to use SOAP? It's not very user friendly. If you're using some system for SMS sending I'm sure it has a REST API.

I don't see where you're actually executing the request to the remote system, or is this just test code?

For XML with namespaces you have to do some more stuff - see http://stackoverflow.com/questions/8669766/namespace-handling-in-groovys-xmlslurper.

SaidAslan November 30, 2016

Hi Jamie, 

Thanks, I've found this a day ago and now the script works. 

This xml is the response of our web service, I've used it as a test case to see how it can be parsed.

def xmlresult = new XmlSlurper().parseText(xml)
assert xmlresult instanceof groovy.util.slurpersupport.GPathResult
def res = xmlresult.Body
 
//return res in Script console or adding a comment in workflow post function
Result.Body returns me OKOK (ErrCode and ErrMsg). Dont know how to separate them. 
 If I try to go deeply into the xml tree ( xmlresult.Body.SMSGateWayResponse ) the system returns that there is not such property. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events