GroovyScript: Parse SOAP Response and get node names and node values
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
GroovyScript: Parse SOAP Response and get node names and node values
Hi all,
I have a SOAP response like this :
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header> <MessageID xmlns="http://www.w3.org">uuid: ...</MessageID> </S:Header> <S:Body> <ns3:ResponseElement xmlns:ns2="http://www.example.com/services/common/example/xsd" xmlns:ns3="http://www.example.com/example/xsd"> <Info> <Data> <Site>OneValue</Site> </Data> <OtherData> <Code>01234</Code> <Time>2017-05-28</Time> <SecondCode>ThirdValue</SecondCode> <Number>000</Number> </OtherData> </Info> <Info> <Data> <Site>OneValue</Site> </Data> <OtherData> <Code>56789</Code> <Time>2017-07-30</Time> <SecondCode>ThirdValue</SecondCode> <Number>111</Number> </OtherData> </Info> </ns3:ResponseElement> </S:Body> </S:Envelope>
And I succeed to get some datas with this code :
def WebServiceResponse = context.expand( '${#[MyWebService]#Response#declare namespace SOAP-ENV=\'http://schemas.xmlsoap.org/soap/envelope/\'; //SOAP-ENV:Body[1]}' ) def ParsingResult = new XmlSlurper().parseText(WebServiceResponse) def Collection = ParsingResult.'**'.collectEntries {!it.childNodes()?[it.name(), it.text()]:[:]} for(int i = 0;i<8;i++) { log.info (Collection.collect()[i].toString()) }
My output :
Tue Jun 06 13:23:00 CEST 2017:INFO: Site=OneValue
Tue Jun 06 13:23:00 CEST 2017:INFO: Code=56789
Tue Jun 06 13:23:00 CEST 2017:INFO: Time=2017-07-30
Tue Jun 06 13:23:00 CEST 2017:INFO: SecondCode=ThirdValue
Tue Jun 06 13:23:00 CEST 2017:INFO: Number=111
But I cannot iterate in the first node "Info" and get its node values. I only get the second part. I want this output :
Site=OneValue
Code=01234
Time=2017-05-28
SecondCode=ThirdValue
Number=000
Site=OneValue
Code=56789
Time=2017-07-30
SecondCode=ThirdValue
Number=111
Could you help me to get a solution for this problem ?
Many thanks in advance.
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nobody could help me ?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Is this what you want? (have refactored it slightly):
def response=''' <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header> <MessageID xmlns="http://www.w3.org">uuid: ...</MessageID> </S:Header> <S:Body> <ns3:ResponseElement xmlns:ns2="http://www.example.com/services/common/example/xsd" xmlns:ns3="http://www.example.com/example/xsd"> <Info> <Data> <Site>OneValue</Site> </Data> <OtherData> <Code>01234</Code> <Time>2017-05-28</Time> <SecondCode>ThirdValue</SecondCode> <Number>000</Number> </OtherData> </Info> <Info> <Data> <Site>OneValue</Site> </Data> <OtherData> <Code>56789</Code> <Time>2017-07-30</Time> <SecondCode>ThirdValue</SecondCode> <Number>111</Number> </OtherData> </Info> </ns3:ResponseElement> </S:Body> </S:Envelope> ''' def slurperResponse = new XmlSlurper().parseText(response) slurperResponse.Body.ResponseElement.Info.each{ log.info it.Data.Site log.info it.OtherData.Code log.info it.OtherData.Time log.info it.OtherData.SecondCode log.info it.OtherData.Number }
You can hopefully just replace response with your WebServiceResponse.
Regards,
Rupert
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You're right, but if I have another parsing response, node names will change. So, I am not able to know node names.
I have to provide a code which is parsing node names and values associated without know them.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
OK, maybe it would help if you say what you actually want to achieve... I had wrongly assumed that you had a structured SOAP response and you wanted to use assertions to maybe check node values etc, but it sounds like you would like a universal way to parse and display arbitrary SOAP responses, is that right?
Thanks,
Rupert
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please if the below script is helpful.
Note that changed node names in the sample xml used for the sake of your comment.
https://github.com/nmrao/groovyScripts/blob/master/xml/GetDataUsingChildren.groovy
Output would be:
[[Site1:OneValue, Code1:01234, Time1:2017-05-28, SecondCode1:ThirdValue, Number1:000], [Site2:OneValue, Code2:56789, Time2:2017-07-30, SecondCode2:ThirdValue, Number2:111]]
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@rupert_anderson that's right, I should not mention any node name in script. I know that I have to use de .children() method but I don't know exactly how to use it ...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @nmrao,
Thanks for your answer, It's almost that !!
The problem is that I should not mention the " it.name()=='Info' ". I'll have other WebServices Response to parse, and node names will change, I can't adapt script on each response.
But many thanks for your answer, I'll try to get a result by using your code !
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
By the way, you do not need to bother about having common script if you are using groovy. It basically depends on data, mostly you can write one-liners here in groovy, so should not really bother about writing common code.
You can find different samples in my github repos.
Regards,
Rao.
