Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 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>

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