Forum Discussion

Dizzo's avatar
Dizzo
Occasional Contributor
8 years ago

Need help with XmlSlurper

Hello guys,

 

I hope you can help me with parsing xml report by Groovy XmlSlurper.

I need to get maximum date of issuance of ID card.

Can anybody help, im noob in this, it's so hard to me.

 

 

String response = """
<Root>
<DateTime>02.01.2017 14:45:21</DateTime>
<ReferenceId>10</ReferenceId>
<ServiceName>Front Office WS</ServiceName>
<ResultCulture>ru-RU</ResultCulture>
<ResultCode>10</ResultCode>
<ResultDescription />
<UsageIdentity />
<Result>
<Root>
<IdentificationDocuments stitle="Documents" title="id documents">
<Document rank="Primary evaluation" stitle="Actual" type="Current">
<Name id="1" title="Document type" value="RNN" />
<DateOfRegistration title="Recording date" value="01.01.1900" />
<DateOfIssuance title="Date of issuance" value="07.08.2002" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Number" value="211567849595" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="12.04.2006" />
</Document>
<Document rank="Primary evaluation" stitle="Actual" type="Current">
<Name id="14" title="Document type" value="IIN" />
<DateOfRegistration title="Recording date" value="15.03.2010" />
<DateOfIssuance title="Date of issuance" value="-" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Number" value="645125901764" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="31.08.2010" />
</Document>
<Document rank="Secondary evaluation" stitle="Actual" type="Current">
<Name id="7" title="Document type" value="ID card" />
<DateOfRegistration title="Recording date" value="20.02.1998" />
<DateOfIssuance title="Date of issuance" value="-" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Number" value="008070236" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="12.04.2006" />
</Document>
<Document rank="Secondary evaluation" stitle="Actual" type="Current">
<Name id="7" title="Document type" value="ID card" />
<DateOfRegistration title="Recording date" value="20.02.1998" />
<DateOfIssuance title="Date of issuance" value="10.10.2010" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Number" value="8057836" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="08.01.2007" />
</Document>
<Document rank="Secondary evaluation" stitle="Actual" type="Current">
<Name id="3" title="Document type" value="SIK" />
<DateOfRegistration title="Recording date" value="01.01.1900" />
<DateOfIssuance title="Date of issuance" value="01.01.1900" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Number" value="E5G7J8846UR5R32U" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="03.06.2007" />
</Document>
<Document rank="Secondary evaluation" stitle="Actual" type="Current">
<Name id="7" title="Document type" value="ID card" />
<DateOfRegistration title="Recording date" value="20.02.1998" />
<DateOfIssuance title="Date of issuance" value="13.05.2015" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Номер" value="ID008070236" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="15.08.2008" />
</Document>
<Document rank="Secondary evaluation" stitle="Actual" type="Current">
<Name id="7" title="Document type" value="ID card" />
<DateOfRegistration title="Recording date" value="11.02.2010" />
<DateOfIssuance title="Date of issuance" value="23.03.2016" />
<DateOfExpiration title="Duration" value="-" />
<Number title="Номер" value="045808572" />
<IssuanceLocation title="Issued by" value="" />
<DateOfInserted title="Date of receipt of information" value="18.03.2010" />
</Document>
</IdentificationDocuments>
</Root>
</Result>
</Root>"""

 

 

 

  • XmlHolder was easier because I wasn't sure about sorting nodes in XmlSlurper.

     

    To filter the nodes and sort the dates in an array

     

    def xml = new XmlSlurper().parseText(response)
    docs = xml.Result.Root.IdentificationDocuments.Document.findAll { it.Name.@value == 'ID card' && it.DateOfIssuance.@value != '-' }
    dates = []
    docs.each {dates.add it.DateOfIssuance.@value.text()}
    log.info dates.sort { Date.parse('dd.MM.yyyy', it ) }.reverse()[0]
  • PaulMS's avatar
    PaulMS
    Super Contributor

     

    import com.eviware.soapui.support.XmlHolder
    holder = new XmlHolder(response)
    dates = holder["//Document[Name[@value='ID card']]/DateOfIssuance[@value!='-']/@value"]
    log.info dates.sort { Date.parse('dd.MM.yyyy', it ) }.reverse()[0]
    • Dizzo's avatar
      Dizzo
      Occasional Contributor

      Thank you, for helping, but how can i do this with out 

       

      import com.eviware.soapui.support.XmlHolder

       

      • PaulMS's avatar
        PaulMS
        Super Contributor

        XmlHolder was easier because I wasn't sure about sorting nodes in XmlSlurper.

         

        To filter the nodes and sort the dates in an array

         

        def xml = new XmlSlurper().parseText(response)
        docs = xml.Result.Root.IdentificationDocuments.Document.findAll { it.Name.@value == 'ID card' && it.DateOfIssuance.@value != '-' }
        dates = []
        docs.each {dates.add it.DateOfIssuance.@value.text()}
        log.info dates.sort { Date.parse('dd.MM.yyyy', it ) }.reverse()[0]