
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - Quering XML
In Groovy, we can query XML intuitively with the help of XmlSlurper. XmlSlurper parses the XML into a navigable structure which can be navigated using dot notations.
Load and Parse existing XML document
Using XmlSlurper we can load the XML document in the application.
def xmlMovies = ''' <movies> <movie title = 'Enemy Behind'> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <director>John Adam</director> <rating>PG</rating> <stars>PG</stars> <description>10</description> </movie> <movie title = 'Transformers'> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <director>Cooper</director> <rating>R</rating> <stars>R</stars> <description>8</description> </movie> </movies> ''' // parse xml into movies object def movies = new XmlSlurper().parseText(xmlMovies) // We can parse XML file as well // def movies = new XmlSlurper().parse(new File('movies.xml'))
Accessing Elements
Now navigate through xml structure and access the elements using dot notation. In case of multiple elements, we can treat them as List.
// access the root name, prints movies println movies.name() // Access all movie element, We gets a List of GPathResult def allMovies = movies.movie // prints Number of movies: 2 println "Number of movies: ${allMovies.size()}" // Access the first movie def movie = movies.movie[0] // print movie type println "Movie type: ${movie.type.text()}"
Accessing Attributes
We can access attributes using @ notation as shown in example below −
// access title of a first movie println movies.movie[0].@title
Iterating elements
We can use each, collect, findall etc. methods to iterate XML elements as shown below −
// Print the title and type of each movie movies.movie.each { mv -> println "Title: ${mv.@title.text()}, Type: ${mv.type.text()}" } // Collect all movie titles def titles = movies.movie.collect { it.@title.text() } println "All titles: $titles"
Filtering elements
We can filter elements easily using findAll. −
// Find all movies in the 'Thriller' category def thrillerMovies = movies.movie.findAll { it.type == 'War, Thriller' } println "Number of thriller movies: ${thrillerMovies.size()}" thrillerMovies.each { println "Thriller book title: ${it.@title.text()}" } // Find movies published after 2000 def recentMovies = movies.movie.findAll { it.year.text().toInteger() > 2000 } println "Recent movie title: ${recentMovies[0].@title.text()}"
Using XPath for Complex Queries
We can use XPath like structure with XmlSlurper depthFirst() or breadthFirst() method in conjuction with findALL() method −
// Find all director elements def directorElements = movies.depthFirst().findAll { it.name() == 'director' } directorElements.each { println "Found Director: ${it.text()}" } // Find a movie of particular director def johnMovie = movies.movie.find { it.director.text() == 'John Adam' } println "Movie by John Adam: ${johnMovie.@title.text()}"
Complete Example
Example.groovy
def xmlMovies = ''' <movies> <movie title = 'Enemy Behind'> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <director>John Adam</director> <rating>PG</rating> <stars>PG</stars> <description>10</description> </movie> <movie title = 'Transformers'> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <director>Cooper</director> <rating>R</rating> <stars>R</stars> <description>8</description> </movie> </movies> ''' // parse xml into movies object def movies = new XmlSlurper().parseText(xmlMovies) // We can parse XML file as well // def movies = new XmlSlurper().parse(new File('movies.xml')) // access the root name, prints movies println movies.name() // Access all movie element, We gets a List of GPathResult def allMovies = movies.movie // prints Number of movies: 2 println "Number of movies: ${allMovies.size()}" // Access the first movie def movie = movies.movie[0] // print movie type println "Movie type: ${movie.type.text()}" // access title of a first movie println movies.movie[0].@title // Print the title and type of each movie movies.movie.each { mv -> println "Title: ${mv.@title.text()}, Type: ${mv.type.text()}" } // Collect all movie titles def titles = movies.movie.collect { it.@title.text() } println "All titles: $titles" // Find all movies in the 'Thriller' category def thrillerMovies = movies.movie.findAll { it.type == 'War, Thriller' } println "Number of thriller movies: ${thrillerMovies.size()}" thrillerMovies.each { println "Thriller book title: ${it.@title.text()}" } // Find movies published after 2000 def recentMovies = movies.movie.findAll { it.year.text().toInteger() > 2000 } println "Recent movie title: ${recentMovies[0].@title.text()}" // Find all director elements def directorElements = movies.depthFirst().findAll { it.name() == 'director' } directorElements.each { println "Found Director: ${it.text()}" } // Find a movie of particular director def johnMovie = movies.movie.find { it.director.text() == 'John Adam' } println "Movie by John Adam: ${johnMovie.@title.text()}"
Output
When we run the above program, we will get the following result.
movies Number of movies: 2 Movie type: War, Thriller Enemy Behind Title: Enemy Behind, Type: War, Thriller Title: Transformers, Type: Anime, Science Fiction All titles: [Enemy Behind, Transformers] Number of thriller movies: 1 Thriller book title: Enemy Behind Recent movie title: Enemy Behind Found Director: John Adam Found Director: Cooper Movie by John Adam: Enemy Behind