Search code examples
groovyxmlslurper

Groovy XMLParser & XMLSlurper


I am working on a groovy xml exercise & as per the problem, i need to write 2 methods:

readFileContent(fileName) - This method is used to read the content from file(xml file) and return that file's content as string.

loadStudents(xmlString) - This method is used to generate the student list using the parameterized xml string(Please parse that xml string(use xmlSlurper) and create the student object and add to the studentList).

i tried using xmlparser.parse(fileName) to create my xmlString, and then used xmlSlurper.parseText in loadStudent method. however, getting content is not allowed prolong error.

Here's my code:

def xmlString  = new XmlParser().parse(fileName)
def students = new XmlSlurper().parseText(xmlString)

here's xml:

<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student>
<id>1</id>
<registerNumber>1225</registerNumber>
<name>Madison</name>
</student>
<student>
<id>3</id>
<registerNumber>4152</registerNumber>
<name>Paul</name>
</student>
<student>
<id>5</id>
<registerNumber>1785</registerNumber>
<name>Sam</name>
</student>
<student>
<id>2</id>
<registerNumber>4158</registerNumber>
<name>Jason</name>
</student>
<student>
<id>4</id>
<registerNumber>1674</registerNumber>
<name>Harry</name>
</student>
</students>

Note: i know i can just directly parse the file in xmlSlurper & it works fine. however, this is online exercise & needs to be done using both methods.

Thanks in advance.


Solution

  • The following code:

    def data     = readFileContent('data.xml')
    def students = loadStudents(data)
    students.each { s ->
      println "${s.name} -> ${s.number}"
    }
    
    class Student { 
      def name 
      def number
    }
    
    String readFileContent(fileName) { 
      new File(fileName).text
    }
    
    def loadStudents(xmlString) { 
      def parser = new XmlSlurper() 
      parser.parseText(xmlString).student.collect { s -> 
        new Student(name: s.name, number: s.registerNumber)
      }
    }
    

    will print:

    Madison -> 1225
    Paul -> 4152
    Sam -> 1785
    Jason -> 4158
    Harry -> 1674
    

    when run on the xml in your example.

    The error you are getting is most probably caused by extra characters at the start of your xml file. This can sometimes also be caused by the parser not allowing the xml declaration at the start of the file. I would start by checking those two things.