Parsing XML in Groovy using XmlSlurper

In my previous post I showed different ways in which we can parse XML document in Java. You must have noticed the code being too much verbose. Other JVM languages like Groovy, Scala provide much better support for parsing XML documents.

In this post I give you the code to parse the XML document in Grooy and one can compare the ease with which we can parse XML documents. I make use of XmlSlurper API in Groovy which loads the complete XML into a tree and this tree can be then navigated using Groovy’s version of XPath called GPath.

The XML document I am using is the same one used here and also the intent is to parse the XML and create a list of Employee object.

class XmlParserDemo {
  static void main(args){
    def empList = new ArrayList<Employee>();
    def emp;
    def employees = 
      new XmlSlurper().parse(ClassLoader.
          getSystemResourceAsStream("xml/employee.xml"));
    employees.employee.each{ node -> 
      emp = new Employee();
      emp.firstName = node.firstName
      emp.lastName = node.lastName
      emp.id = node.@id
      emp.location = node.location
      empList.add(emp)
    }
    empList.each{ empT -> println(empT)}
  }
}
class Employee{
  String firstName
  String lastName
  String id
  String location
  
  @Override
  public String toString(){
    return "${firstName} ${lastName}(${id}) in ${location}"
  }
}

The output is:

Rakesh Mishra(111) in Bangalore
John Davis(112) in Chennai
Rajesh Sharma(113) in Pune

And the XML is present in the “xml” package and is available in the classpath of the application. I am using the ClassLoader to load the XML resource.

Sample application using JavaFX 2.0 beta and the after thoughts

This sample uses- TabPane, GridView and Bindings in javafx 2.0.

I had quite sometime back played around with JavaFX and had good and bad experiences using the language. With the JavaFX 2.0 beta being released I thought of giving it a try. Here I developed a simple Geocoding application which will take the address and provide the latitude-longitude values for that location- using Google Geocoding API. Continue reading “Sample application using JavaFX 2.0 beta and the after thoughts”

Parsing XML is fun again- thanks to Groovy

I happened to work on Groovy while I was developing using Gaelyk and I found Groovy lot easier to write- I never thought of the types and this really helped me to make my codeGroov simpler and easier to write. And when you are dealing with RESTful APIs- you ought to be parsing XML,JSON and what not. As a Java programmer- I never enjoyed it. But Groovy really made parsing xml fun again. Continue reading “Parsing XML is fun again- thanks to Groovy”

Selection Sort: Brute Force approach to sorting with example in Groovy

In Selection sort technique the list is scanned to find the smallest element and it is then exchanged with the first element, putting the smallest element in its final position i.e at the beginning. Then the list is scanned starting from the second element to find the smallest among the remaining n-1 elements and exchange it with the second element to put the second smallest element in its final position. In general in the ith pass the algorithm searches for the smallest number from the n-i elements and swaps it with the element in the ith position i.e Ai. The list is sorted after n-1 passes. Continue reading “Selection Sort: Brute Force approach to sorting with example in Groovy”