Ask a Question

Output some selected parameters of XML response to a log file

SOLVED
Mis3
Frequent Contributor

Output some selected parameters of XML response to a log file

I tried to output certain parameters of a XML response (below) to a log file.

The parameters I like to output:  CoSID, PIN, iPhoneDevice, etc.   Not sure how to do this.

 

def xmlResponse = tStep.getPropertyValue("Response")
def xml = new XmlSlurper().parseText(xmlResponse)

 

I tried a few combinations of the following 2 lines with no success.   

def MESSAGE2 = xml.Body.ShPullResponse.UserData.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.CoSID.text()
// def MESSAGE2 = xml.Body.ShPullResponse.@COSID.text()

log.info MESSAGE2

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns1:ShPullResponse xmlns:ns1="http://www.metaswitch.com/srb/soap/sh">
<ns1:ResultCode>2001</ns1:ResultCode>
<ns1:ExtendedResult>
<ns1:ExtendedResultCode>100</ns1:ExtendedResultCode>
<ns1:ExtendedResultDetail>Request successful</ns1:ExtendedResultDetail>
<ns1:ExtendedSubResults/>
</ns1:ExtendedResult>
<ns1:UserData>
<ud:Sh-Data xmlns:ud="http://www.metaswitch.com/srb/soap/sh/userdata" xmlns:u="http://www.metaswitch.com/sdp/soap/sh/userdata">
<ud:RepositoryData>
<ud:ServiceIndication>Msph_Subscriber_BaseInformation</ud:ServiceIndication>
<ud:SequenceNumber>5385</ud:SequenceNumber>
<ud:ServiceData>
<s:MetaSphereData xmlns:s="http://www.metaswitch.com/sdp/soap/sh/servicedata">
<s:Msph_Subscriber_BaseInformation>
<s:DisplayName>Pierre</s:DisplayName>
<s:CoSID>English_VVM_CoS</s:CoSID>
<s:EmailAddress>3313140097@xplornet.com</s:EmailAddress>
<s:Disabled>False</s:Disabled>
<s:DisabledforMigration>False</s:DisabledforMigration>
<s:PrimaryPhoneNumber>3313140097</s:PrimaryPhoneNumber>
<s:PIN>1234</s:PIN>
<s:PreferredLanguage>eng</s:PreferredLanguage>
<s:BillingNumber>3313140097</s:BillingNumber>
<s:Surname>Pierre</s:Surname>
<s:TimeZone>America/Winnipeg</s:TimeZone>
<s:Groupname>TestiPhoneVVM</s:Groupname>
<s:Device>
<s:PhoneNumber>4313140097</s:PhoneNumber>
<s:AnsweringService>lm</s:AnsweringService>=
<s:iPhoneDevice>True</s:iPhoneDevice>
</s:Device>
<s:Password>$Allan****</s:Password>
<s:VideoMessagingEnabled>True</s:VideoMessagingEnabled>
<s:SelfProvStatus>Not applicable</s:SelfProvStatus>
<s:ClusterName/>
<s:InvariantUID>20210904033825FAB24313140097</s:InvariantUID>
</s:Msph_Subscriber_BaseInformation>
</s:MetaSphereData>
</ud:ServiceData>
</ud:RepositoryData>
</ud:Sh-Data>
</ns1:UserData>
</ns1:ShPullResponse>
</soapenv:Body>
</soapenv:Envelope>

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: Output some selected parameters of XML response to a log file

Looks the data fields are unique in this case; so easy to extract with a simple closure.

 

nmrao_0-1631002403056.png

 

def getData = { item -> xml.'**'.find{it.name() == item }?.text() }
//You may use log.info or assign value instead of print if you want to process that info further.
println getData('ResultCode')
println getData('ExtendedResultCode')
println getData('CoSID')
println getData('PIN')
println getData('iPhoneDevice')

 

 

 



Regards,
Rao.

View solution in original post

15 REPLIES 15
Mis3
Frequent Contributor

Re: Output some selected parameters of XML response to a log file

I tried the following lines in the Groovy.

So far, I can output only MESSAGE1 and MESSAGE2 properly but not the rest.  In other words, I cannot access any data below ExtendedResult in the XML response.

Any suggestion is appreciated.   Thanks.

 

def MESSAGE1 = xml.Body.ShPullResponse.ResultCode.text()
def MESSAGE2 = xml.Body.ShPullResponse.ExtendedResult.ExtendedResultCode.text()

// def MESSAGE3 = xml.Body.ShPullResponse.UserData.SequenceNumber.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.@SequenceNumber.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.RepositoryData.SequenceNumber.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.CoSID.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.@CoSID.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.Sh_Data.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.CoSID.text()
def MESSAGE3 = xml.Body.UserData.ServiceData.MetaSphereData.CoSID.text()
// def MESSAGE3 = xml.Body.ShPullResponse.UserData.@COSID.text()

richie
Community Hero

Re: Output some selected parameters of XML response to a log file

Hey @@Mis3,

Im on my phone so cant see to check properly but i notice you are using the @ symbol in your xpath on element names. The @ symbol is oy used to specify attributes of an element. That is, id youre apecifying an element dont use the @ symbol.

Also, xpath is case sensitive so make sure you follow the upper/lower case exactly how it appears in your xml.

I'll have a look tomorrow when im on my laptop,

Cheers

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Mis3
Frequent Contributor

Re: Output some selected parameters of XML response to a log fileh

Thanks , Richie. Please take a look when you have the chance 
As shown in my 1st post, the XML response is a bit complicated.  I thought I could use the @ symbol because all the parameter names are unique.    
Anyway, in my 2nd post, I listed everything I tried.  I did try with the @ symbol and without. 

richie
Community Hero

Re: Output some selected parameters of XML response to a log fileh

Hey @Mis3 

 

Double check your xpath - that's where the problems are

 

for example - you've specified the path to the SequenceNumber element as --> xml.Body.ShPullResponse.UserData.SequenceNumber

 

However, the xpath to SequenceNumber is as follows:

 

xml.Body.ShPullResponse.UserData.Sh-Data.RepositoryData.SequenceNumber

 

Your code is gonna be fine if you've managed to get it working for the 2 MESSAGES - so I reckon you just need to fix your xpath properly.

 

If you haven't got anything like XMLSpy or equivalent, you can try --> https://www.easycodeforall.com/generate-xpath.html to generate your xpaths and this --> https://www.freeformatter.com/xml-formatter.html to pretty print your payload out which makes visualising how your xpath is setup easier.

 

hit me back if you have any further problems, but I suspect you won't!

 

Cheers

 

rich

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Mis3
Frequent Contributor

Re: Output some selected parameters of XML response to a log fileh

I tried using this:  

def MESSAGE3 = xml.Body.ShPullResponse.UserData.Sh-Data.RepositoryData.SequenceNumber

It failed with error "groovy.lang.MissingPropertyException:No such property:Data for class:Script8 error at line:59"

 

I actually tried this line before I posted this in the forum.    I think somehow the Groovy does not like the "-" in Sh-Data.  

 

For example, the following 2 lines works. 

def MESSAGE1 = xml.Body.ShPullResponse.ResultCode.text()
def MESSAGE2 = xml.Body.ShPullResponse.ExtendedResult.ExtendedResultCode.text()

Somehow I cannot access any parameters below Sh-Data. 

 

 

 

 

nmrao
Community Hero

Re: Output some selected parameters of XML response to a log file

Looks the data fields are unique in this case; so easy to extract with a simple closure.

 

nmrao_0-1631002403056.png

 

def getData = { item -> xml.'**'.find{it.name() == item }?.text() }
//You may use log.info or assign value instead of print if you want to process that info further.
println getData('ResultCode')
println getData('ExtendedResultCode')
println getData('CoSID')
println getData('PIN')
println getData('iPhoneDevice')

 

 

 



Regards,
Rao.

View solution in original post

Mis3
Frequent Contributor

Re: Output some selected parameters of XML response to a log file

I tried and it is working great. 

Since the responses of most of the APIs (with a few exceptions)  have unique attribute names, this getData function will work great for me.

 

For my education:

1.  I tried this statement:  

def MESSAGE3 = xml.Body.ShPullResponse.UserData.Sh-Data.RepositoryData.SequenceNumber and it failed with error "groovy.lang.MissingPropertyException:No such property:Data for class:Script8 error at line:59"

Looks to me that the Groovy does not like the hyphen in Sh-Data.    Since this Sh-Data is present in the XML response, why does this fail?

2.  I thought if the XML response have unique attribute names, I can use the "@" sign to retrieve the attribute value.

In this case:

// def MESSAGE3 = xml.Body.ShPullResponse.UserData.@SequenceNumber.text()

No error from Groovy but it does not provide me with the SequenceNumber.

 

 

 

 

nmrao
Community Hero

Re: Output some selected parameters of XML response to a log file

1. Element name 'Sh-Data' is not a standard practice, I guess. You can wrap it between quotes.
2. In your data, there is no attribute. So simply ignored.


Regards,
Rao.
Mis3
Frequent Contributor

Re: Output some selected parameters of XML response to a log file

1.  I did wrap Sh-Data in quotes and it works.

def MESSAGE1 = xml.Body.ShPullResponse.UserData.'Sh-Data'.RepositoryData.SequenceNumber
def MESSAGE2 = xml.Body.ShPullResponse.UserData.'Sh-Data'.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.CoSID
def MESSAGE3 = xml.Body.ShPullResponse.UserData.'Sh-Data'.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.PIN
def MESSAGE4 = xml.Body.ShPullResponse.UserData.'Sh-Data'.RepositoryData.ServiceData.MetaSphereData.Msph_Subscriber_BaseInformation.Device.iPhoneDevice

These lines gives me the proper result of each parameter.

Of course, I prefer the getData; it is simpler since I do not have to specify the full path.

 

2.  I think I have fundamental misunderstanding of XML nomenclature.  

For me,  in <s:CoSID>English_COS2</s:CoSID>  -->  CoSID is an attribute and English-COS2 is the value.

I did google.  The whole line is called an element; including open and close tags.

So, what do you call CoSID and English-COS2?

 

 

 

cancel
Showing results for 
Search instead for 
Did you mean: