Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 years ago

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>

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

     

     

    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')
    

     

     

     

  • Mis3's avatar
    Mis3
    Frequent Contributor

    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's avatar
      richie
      Community Hero
      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
      • Mis3's avatar
        Mis3
        Frequent Contributor

        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. 

  • nmrao's avatar
    nmrao
    Champion Level 3

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

     

     

    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')
    

     

     

     

    • Mis3's avatar
      Mis3
      Frequent Contributor

      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's avatar
        nmrao
        Champion Level 3
        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.