Ask a Question

How to iterate through XML response with Groovy

New Contributor

How to iterate through XML response with Groovy

I'm new to Groovy scripting and right now i'm trying to accomplish a task which requires me to capture multiple data from an xml and convert it to such as a txt file or an excel file. I'm stuck at the step that how would i go about capture dynamic XML structure that might change in number every time i perform data source loop? 


Better example: 

I have a below XML which is from a respond 



<available-document >
<document-code >123456</document-code >
<document-description lang="en">Document one name </document-description>

<document-description lang="ko">Document two name</document-description>


so far I can capture all the values in a group by using below code: 


for( item in holder.getNodeValues( "//document-description" ))
// loop item nodes in response message "Document name : [$item]"
file <<"|$item"


however that since each time the <document-group> number of times it appears in <available-document > changes, it will require me to capture all the <document-group> under <available-document >, the print out could look something like this.


code: 123456

group: 2, 23, 101

doc-desc: Document one name

date: 2013-08-22


code: 654321

group: 6

doc-desc: Document two name

date: 2013-08-30


my final plan is to use all these data and compare to the application UI by storing all these data in a txt file and read them through Test Complete , performing string manipulation to achieve the  goal of comparing the data from XML to Application UI. 


Thank you in advance, any help would be greatly appreciated 



Regular Contributor

Re: How to iterate through XML response with Groovy

I suspect, you are iterating it in a wrong node position. Instead of this - 

for( item in holder.getNodeValues( "//document-description" ))

you should move up one level and should iterate as shown below,

for( item in holder.getNodeValues( "//available-document" ))

Again, for the change in number of occurrences of <document-group> , the getNodeValues() method is capable of returning a String array, where you can compute for to get comma separated values.




New Contributor

Re: How to iterate through XML response with Groovy

Hello Kondasamy, 


Thank you for your response, I get the idea that I can use holder.getNodeValues("//document-group") to retrieve all the document-group in the XML, but how would i go about retrieve just the <document-group> that are within the <available-document> ?  the reason is that I will have multiple <available-document> and the number of time it appears in the response will change, now the idea is to iterate through all the <available-document> in the XML and in each <available document> i would like to iterate through the XML and retrieve the  <document-group> that are in it. I wasnt having any solution on how would I go about nested iteration. 


Thank you






Re: How to iterate through XML response with Groovy

You can check XMLSlurper,



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Showing results for 
Search instead for 
Did you mean: