Grails Cookbook - A collection of tutorials and examples

Groovy XmlSlurper Examples for Parsing XML

One common task in an enterprise application is parsing of XML documents. This task is commonly complicated in Java with lots of plumbing code, but is fairly easy and straightforward in Groovy. Below are examples on how to parse XML in Groovy using XmlSlurper. Visit this post if you wish to parse XML using XmlParser.

Groovy Switch Statement Examples

The Switch statement in Groovy is more powerful than in Java. There are things that are easy to do in Groovy switch statements that are not available in Java. It is also more concise and easier to read. Below are several examples on how to use Groovy switch statements.

Groovy List Tutorial And Examples

A list is a common data structure that is used to store a collection of data. This can be used to store varying amount of data where the number of items is only determined at run time. Or it can also be used to store large amount of data where having different variables for each is not practical. Below are examples on how to use and manipulate List in Groovy.

Groovy Substring Examples

Getting a substring from another String is one of the basic building blocks of String manipulation. Most programming languages supports it out of the box. In Groovy, there are some cool ways of how to perform this operation. Below are some examples on different ways of getting substrings in Groovy.

Groovy Add To Map Examples

Maps are very useful in programming. It can be used to cache results of expensive operations to help improve performance. One of the basic operation in maps is how to add entries to it. Below are some examples on how to Add or put values to a map in Groovy.

Groovy Array Length

Array manipulation is common in any programming language. Here is a sample code on how to get the the length of an array in Groovy.

Groovy Copy File Examples

When writing batch programs, it is common to write code to copy files from one directory to another. Here are examples on how to Copy Files in Groovy.

Groovy FindAll

Groovy is popular for writing short and concise codes. Programs that contains many lines of code using Java can be shortened in Groovy. This greatly helps in developer productivity. A feature that is commonly used in Groovy is in searching for items in a collection. The Groovy FindAll method searches for a subset of a collection that matches a given criteria. Here are some examples on how to use Groovy FindAll:

Groovy Find

Groovy is one of the coolest language available because it makes coding so much easier. Long Java codes can be written in Groovy in fewer lines that are much more readable. One of the most used feature in Groovy is in searching. The Groovy Find method can be used to search for a single object in a collection, given a condition on what needs to be searched for. Below are some examples on how to use Groovy Find for searching.

Groovy Def Declaration Examples

When we declare a variable or function in Groovy, we can use the def keyword to define the type. The Groovy Def can be used if we don't want to restrict the type of an object. Hence, we can change what can be assigned to a variable any time during runtime. When we use Groovy def to declare something, it is similar to declaring it as an Object class. Because the class Object is the root class of all classes, you can assign anything to a variable of type Object. Here are some examples on how to use def in Groovy.