Showing results for 
Search instead for 
Do you mean 
Reply
Occasional Contributor
Posts: 5
Registered: ‎02-06-2017
Accepted Solution

Groovy XML Script for Loops

Hi:

 

I have an example XML as follows:

 

<Element>

<row></row>

<row></row>

<row></row>

<row></row>

<row></row>

<row></row>

<row></row>

<row></row>

</Element>

 

Can someone suggest a syntax to loop around this XML, say from rows 4-6 only?

 

I have tried a For-each loop, that that seems to iterate thru all rows.

 

Thx

Highlighted
Community Hero
Posts: 3,846
Registered: ‎05-17-2013

Re: Groovy XML Script for Loops

[ Edited ]

You may want to add more details on what you are trying to achieve. If you have some realistic data, there might have some patterns or an alternative easy ways could exists.

Here is a simple one to demonstrate. In the list, the index starts with 0, so taking 3-5 (instead of 4-6)

def list = ['a1','a2','a3','a4','a5','a6']
(3..5).each { index -> log.info list[index]}

Regards,
Rao.
Occasional Contributor
Posts: 5
Registered: ‎02-06-2017

Re: Groovy XML Script for Loops

Thanks for the response.

 

I am looking for a way when I need to generate an XML from another XML Response(s). In example below, I have a  student list of 50 students and a teacher list of 3 teachers. I want an sections assingnments XML as the output where each teacher is assigned to a section, and upto 20 students are assigned in that section.

 

 

So I need to loop on each teacher element, and for each teacher element, loop on the student name from 1-20, than for 2nd teacher, students 21-40 etc.

 

Ex: I have a XML list of students, say a simple format as follows:

       <Students>

                <name> st name 1</name>

                <name> st name 2</name>

                <name> st name 3</name>

                ..

                ..

                 <name> st name 50</name>

       </Students>

 

And XML list of teachers:

       <Teachers>

                <name> Teacher name 1</name>

                <name> Teacher name 2</name>

                <name> Teacher name 3</name>

                 <name> st name 50</name>

       </Teachers>

 

 

I want a result to look as follows:

        <Assignments>

                 <Section>

                          <id>1 </id>

                          <teacher>Teacher name 1</teacher>

                           <studentList>

                                     <name> st name 1</name>

                                      <name> st name 2</name>

                                       ..

                                       ..

                                    <name> st name 20</name>

                            </studentList>

                </Section> 

                 <Section>

                          <id>2 </id>

                          <teacher>Teacher name 2</teacher>

                           <studentList>

                                     <name> st name 21</name>

                                      <name> st name 22</name>

                                       ..

                                       ..

                                    <name> st name 40</name>

                            </studentList>

                </Section>   

                

                 <Section>

                          <id>3 </id>

                          <teacher>Teacher name 3</teacher>

                           <studentList>

                                     <name> st name 41</name>

                                      <name> st name 42</name>

                                       ..

                                       ..

                                    <name> st name 50</name>

                            </studentList>

                </Section>   

     

Community Hero
Posts: 3,846
Registered: ‎05-17-2013

Re: Groovy XML Script for Loops

[ Edited ]

You could have provided above information for the first time itself. Why doing it in multi-times? Hoping that it is at least provided the nearest sample to the actual values this time.

 

Here is the script:

https://github.com/nmrao/groovyScripts/blob/master/xml/BuildXmlFromMultipleXmls.groovy

 

or http://groovyconsole.appspot.com/script/5148948125712384 ==> here you can also "execute script" online

 

Note you may replace "println" with "log.info" if you want to see the output in soapui.

 

Output :

Selection_331.png

 

 

Regards,
Rao.