'No such property for class' using XmlSlurper

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 vote

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.

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

38 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you