Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
3 years ago
Solved

XML Response with duplicate element names

Below is the API response I try to parse.   

 

First method:

def MESSAGE1 = xml.Body.GetSubscriberResponse.errorCode.text()

def MESSAGE4 = xml.Body.GetSubscriberResponse.subscriber.service.code.text()
def MESSAGE5 = xml.Body.GetSubscriberResponse.subscriber.avp.value.text()

Report file = 09/11/2021 12:42:58 Seq=18,14313140097, Status=[0] DATAROAMINGVoLTESOS,Test21Test22Test1Test2Test3

 

Second method:

def getData = { childElement, parentElement = null ->
if (parentElement)
{ xml.'**'.findAll {it.name() == parentElement}."$childElement"*.text() }
else
{ xml.'**'.findAll {it.name() == childElement}*.text()} }
 def list = ["\n"+ sdf.format(log_date)+" Seq="+i+","+ data[0],", Status=${getData('errorCode')}", ",", getData('code','service'),",", getData('value','avp')] getData('code','service'),",", getData('value','avp')]
log.info list.flatten().join('')
report << list.flatten().join('')

The report file = 09/11/2021 12:43:25 Seq=10,14313140097, Status=[0],DATAROAMINGVoLTESOS,Test21Test22Test1Test2Test3

 

So, the 2 methods yield the same results.  I like to improve the readability of the report file.

Like:

For  service, it now outputs DATAROAMINGVoLTESOS, how do I output: DATA-ROAMING-VoLTE-SOS

For avp, it now outputs Test21Test22Test1Test2Test3, how do I output Test21-Test22-Test1-Test2-Test3

 

Thanks.

 

-----------------------------------------------------------------------------------------------------------------

<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">
<se:Body>
<GetSubscriberResponse xmlns="http://broadhop.com/unifiedapi/soap/types">
<errorCode>0</errorCode>
<errorMessage>Request completed successfully</errorMessage>
<subscriber>
<id>00600000503bef1e61127f35</id>
<name>
<fullName>PTPT</fullName>
</name>
<credential>
<networkId>14313140097</networkId>
<type>MSISDN</type>
</credential>
<service>
<code>DATA</code>
<enabled>true</enabled>
</service>
<service>
<code>ROAMING</code>
<enabled>true</enabled>
</service>
<service>
<code>VoLTE</code>
<enabled>true</enabled>
</service>
<service>
<code>SOS</code>
<enabled>true</enabled>
</service>
<status>ACTIVE</status>
<role>READ_ALL</role>
<billingInfo/>
<avp>
<code>profileKey1</code>
<value>Test21</value>
</avp>
<avp>
<code>profileKey1</code>
<value>Test22</value>
</avp>
<avp>
<code>profileKey</code>
<value>Test1</value>
</avp>
<avp>
<code>profileKey</code>
<value>Test2</value>
</avp>
<avp>
<code>profileKey</code>
<value>Test3</value>
</avp>
<version>15</version>
</subscriber>
</GetSubscriberResponse>
</se:Body>
</se:Envelope>

----------------------------------------------------------------------------------------------------------------------

  • Try to include the separator between the quotes of join.

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Try to include the separator between the quotes of join.
    • Mis3's avatar
      Mis3
      Frequent Contributor

      As per your suggestion, I tried the following.   Unfortunately, this introduces too many "-" in the report file.  I will have to do some trial-and-errors.

       

      def getData = { childElement, parentElement = null ->
      if (parentElement)
      { xml.'**'.findAll {it.name() == parentElement}."$childElement"*.text() }
      else
      { xml.'**'.findAll {it.name() == childElement}*.text()} }
      def list = ["\n"+ sdf.format(log_date)+" Seq="+i+","+ data[0],",", msisdn_1,", Status=${getData('errorCode')}", ",", getData('fullName'),",", getData('networkId'),",", getData('code','service'),",", getData('value','avp')]
      log.info list.flatten().join('-')

      report << list.flatten().join('-')

       

      Report file:

      09/13/2021 16:26:12 Seq=20,14313140097-,-14313140097-, Status=[0]-,-PTPT-,-14313140097-,-DATA-ROAMING-VoLTE-SOS-,-Test21-Test22-Test1-Test2-Test3

      • Mis3's avatar
        Mis3
        Frequent Contributor

        Much better now.   Thanks all.

        New Groovy:

        def MESSAGE1 = xml.Body.GetSubscriberResponse.errorCode.text()
        def MESSAGE2 = xml.Body.GetSubscriberResponse.subscriber.name.fullName.text()
        def MESSAGE3 = xml.Body.GetSubscriberResponse.subscriber.credential.networkId.text()
        def MESSAGE4 = getData('code','service').flatten().join('-')
        def MESSAGE5 = getData('value','avp').flatten().join('-')

        Report file:

        09/14/2021 08:07:52 Seq=5 14313140097,14313140097,Result:0,Pierre,14313140097,DATA-ROAMING-VoLTE-SOS,NORMAL-Test2-Test3-XM200