cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with XmlSlurper

SOLVED
Highlighted
Occasional Contributor

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>"""

 

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Super Contributor

Re: Need help with XmlSlurper

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]

View solution in original post

Highlighted
Community Hero

Re: Need help with XmlSlurper

@Dizzo,

Looks got what you mean to say.

Below Script Assertion would show id and max DateOfIssue.

https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/xml/QueryRelativeData.groovy

 

You can quickly try it online as well using below link.

https://ideone.com/z8Rg76



Regards,
Rao.

View solution in original post

14 REPLIES 14
Highlighted
Super Contributor

Re: Need help with XmlSlurper

 

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]
Highlighted
Occasional Contributor

Re: Need help with XmlSlurper

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

 

import com.eviware.soapui.support.XmlHolder

 

Highlighted
Community Hero

Re: Need help with XmlSlurper

@Dizzo, what do you mean by " maximum date of issuance of ID card."
Can you elaborate with sample data(expected output) from the above xml?


Regards,
Rao.
Highlighted
Super Contributor

Re: Need help with XmlSlurper

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]

View solution in original post

Highlighted
Community Hero

Re: Need help with XmlSlurper

@Dizzo,

Looks got what you mean to say.

Below Script Assertion would show id and max DateOfIssue.

https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/xml/QueryRelativeData.groovy

 

You can quickly try it online as well using below link.

https://ideone.com/z8Rg76



Regards,
Rao.

View solution in original post

Highlighted
Occasional Contributor

Re: Need help with XmlSlurper

In this xml  a lot of different documents, i need just one value (latest) of dateOfIssuance where document name = ID card.

Latest date is 23.03.2016.

Highlighted
Occasional Contributor

Re: Need help with XmlSlurper

Hello

 

This part doesn't work Smiley Sad

log.info dates.sort { Date.parse('dd.MM.yyyy', it ) }.reverse()[0]

 Caught: groovy.lang.MissingPropertyException: No such property: log for class: xmlParse1
groovy.lang.MissingPropertyException: No such property: log for class: xmlParse1
at xmlParse1.run(xmlParse1.groovy:91)

Highlighted
Community Hero

Re: Need help with XmlSlurper

@Dizzo, have you check my reply?


Regards,
Rao.
Highlighted
Occasional Contributor

Re: Need help with XmlSlurper

Yes, i checked, it looks almost as needed.

Can you make it just for key 7. I need just one value, not array.

Thank you a lot.

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors