Forum Discussion

feroz_k's avatar
feroz_k
Occasional Contributor
8 years ago

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

  • nmrao's avatar
    nmrao
    Champion Level 3

    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]}

    • feroz_k's avatar
      feroz_k
      Occasional Contributor

      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>