Diganta
Diganta

Reputation: 345

Insert comma as separator while adding to list

I have a xml as follows (test.xml):

<Test>
  <ClassId Value="11" Name="">
    <StudentName>John Doe</StudentName>
  </ClassId>
  <ClassId Value="15" Name="">
    <StudentName>Carl Lewis</StudentName>
    <StudentName>Usain Bolt</StudentName>
    <StudentName>Super User</StudentName>
  </ClassId>
  <ClassId Value="52" Name="">
    <StudentName>Marco Senna</StudentName>
    <StudentName>Luis Suarez</StudentName>
  </ClassId>
</Test>

I want to retrieve the as a comma separated list. My code is as follows:

def list = []
def parser = new XmlSlurper().parse(test.xml)
parser.ClassId.each {
    list << it.StudentName
}

The list is as follows:
[John Doe, Carl LewisUsain BoltSuper User, Marco SennaLuis Suarez]
But instead of above I need the list as follows:
[John Doe, Carl Lewis, Usain Bolt, Super User, Marco Senna, Luis Suarez]

Upvotes: 0

Views: 212

Answers (1)

tim_yates
tim_yates

Reputation: 171084

Not at a computer, but I believe:

parser.ClassId.StudentName*.text()

Should return the list you want

Upvotes: 1

Related Questions