
- •Credits
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introduction
- •Installing Groovy on Windows
- •Installing Groovy on Linux and OS X
- •Executing Groovy code from the command line
- •Using Groovy as a command-line text file editor
- •Running Groovy with invokedynamic support
- •Building Groovy from source
- •Managing multiple Groovy installations on Linux
- •Using groovysh to try out Groovy commands
- •Starting groovyConsole to execute Groovy snippets
- •Configuring Groovy in Eclipse
- •Configuring Groovy in IntelliJ IDEA
- •Introduction
- •Using Java classes from Groovy
- •Embedding Groovy into Java
- •Compiling Groovy code
- •Generating documentation for Groovy code
- •Introduction
- •Searching strings with regular expressions
- •Writing less verbose Java Beans with Groovy Beans
- •Inheriting constructors in Groovy classes
- •Defining code as data in Groovy
- •Defining data structures as code in Groovy
- •Implementing multiple inheritance in Groovy
- •Defining type-checking rules for dynamic code
- •Adding automatic logging to Groovy classes
- •Introduction
- •Reading from a file
- •Reading a text file line by line
- •Processing every word in a text file
- •Writing to a file
- •Replacing tabs with spaces in a text file
- •Deleting a file or directory
- •Walking through a directory recursively
- •Searching for files
- •Changing file attributes on Windows
- •Reading data from a ZIP file
- •Reading an Excel file
- •Extracting data from a PDF
- •Introduction
- •Reading XML using XmlSlurper
- •Reading XML using XmlParser
- •Reading XML content with namespaces
- •Searching in XML with GPath
- •Searching in XML with XPath
- •Constructing XML content
- •Modifying XML content
- •Sorting XML nodes
- •Serializing Groovy Beans to XML
- •Introduction
- •Parsing JSON messages with JsonSlurper
- •Constructing JSON messages with JsonBuilder
- •Modifying JSON messages
- •Validating JSON messages
- •Converting JSON message to XML
- •Converting JSON message to Groovy Bean
- •Using JSON to configure your scripts
- •Introduction
- •Creating a database table
- •Connecting to an SQL database
- •Modifying data in an SQL database
- •Calling a stored procedure
- •Reading BLOB/CLOB from a database
- •Building a simple ORM framework
- •Using Groovy to access Redis
- •Using Groovy to access MongoDB
- •Using Groovy to access Apache Cassandra
- •Introduction
- •Downloading content from the Internet
- •Executing an HTTP GET request
- •Executing an HTTP POST request
- •Constructing and modifying complex URLs
- •Issuing a REST request and parsing a response
- •Issuing a SOAP request and parsing a response
- •Consuming RSS and Atom feeds
- •Using basic authentication for web service security
- •Using OAuth for web service security
- •Introduction
- •Querying methods and properties
- •Dynamically extending classes with new methods
- •Overriding methods dynamically
- •Adding performance logging to methods
- •Adding transparent imports to a script
- •DSL for executing commands over SSH
- •DSL for generating reports from logfiles
- •Introduction
- •Processing collections concurrently
- •Downloading files concurrently
- •Splitting a large task into smaller parallel jobs
- •Running tasks in parallel and asynchronously
- •Using actors to build message-based concurrency
- •Using STM to atomically update fields
- •Using dataflow variables for lazy evaluation
- •Index

5
Working with XML in Groovy
In this chapter, we will cover the following recipes:
ff
ff
ff
ff
ff
ff
ff
ff
ff
Reading XML using XmlSlurper Reading XML using XmlParser
Reading XML content with namespaces Searching in XML with GPath Searching in XML with XPath Constructing XML content
Modifying XML content Sorting XML nodes
Serializing Groovy Beans to XML
Introduction
Dealing with XML in Java is notoriously a tedious business. The Java architects traded flexibility for simplicity, and the Java XML APIs are considered among the lower peaks of the language. Fortunately, Groovy offers a marvelous alternative for reading and producing XML. It's so good that once you experience Groovy's native parsers and emitters, you'll wonder why you used anything else.
www.it-ebooks.info

Working with XML in Groovy
This chapter is divided into recipes that deal with reading XML and producing XML. The two Groovy parsers, XmlParser and XmlSlurper, are discussed in detail as well as the two
XML producers, MarkupBuilder and StreamingMarkupBuilder. We also touch on advanced topics such as the serialization of Java and Groovy objects to XML, and element tree navigation with XPath and GPath.
Reading XML using XmlSlurper
The eXtensible Markup Language or simply XML is the standard data format for exchanging information among computer systems. The first two recipes of this chapter show how to parse XML using Groovy. There are two parsers available in the groovy.util package, XmlParser and XmlSlurper. They both expose similar API; but there are use cases for when it is more appropriate to use one or the other. In this recipe, we look at how to read XML with XmlSlurper and its main peculiarities.
Getting ready
For the examples in the rest of this recipe, we will work with an XML document (shown in the following code) containing a list of works from William Shakespeare. The document is named
shakespeare.xml:
<?xml version="1.0" ?>
<bib:bibliography xmlns:bib="http://bibliography.org" xmlns:lit="http://literature.org">
<bib:author>William Shakespeare</bib:author> <lit:play>
<lit:year>1589</lit:year>
<lit:title>The Two Gentlemen of Verona.</lit:title> </lit:play>
<lit:play>
<lit:year>1594</lit:year>
<lit:title>Love's Labour's Lost.</lit:title> </lit:play>
<lit:play>
<lit:year>1594</lit:year> <lit:title>Romeo and Juliet.</lit:title>
</lit:play>
<lit:play>
<lit:year>1595</lit:year>
<lit:title>A Midsummer-Night's Dream.</lit:title> </lit:play>
</bib:bibliography>
168
www.it-ebooks.info

Chapter 5
How to do it...
Let's go through the process of parsing the previously mentioned XML file:
1.One way to read XML data using XmlSlurper is to create an instance of the class and pass a java.io.File object, which references the file we want to read, into the parse method:
def xmlSource = new File('shakespeare.xml')
def bibliography = new XmlSlurper().parse(xmlSource)
2.The parse method returns an implementation of groovy.util. slurpersupport.GPathResult, which can be used to navigate the XML element tree. For example, the following code will print the text representation of the author element:
println bibliography.author
3.Deeper elements and element collections can also be referenced with the help of the "." operator. Also, a set of finder and iterator methods are available to build complex search expressions:
bibliography.play
.findAll { it.year.toInteger() > 1592 }
.each { println it.title }
The expressions that are used to navigate (and eventually also modify) the XML tree are referred to as GPath expressions. More examples of those expressions can be found in the Searching in XML with GPath recipe.
4.The output of the script should be as follows:
William Shakespeare Love's Labour's Lost. Romeo and Juliet.
A Midsummer-Night's Dream.
How it works...
The previous example selects all the plays written after 1592 and prints their titles.
Groovy's XmlSlurper resides in the groovy.util package, which is imported automatically by Groovy. That's why we do not need an import statement for that class.
169
www.it-ebooks.info

Working with XML in Groovy
XmlSlurper is a SAX-based parser; it loads the full document in memory, but it doesn't require extra memory to process the document using GPath. GPath expressions are lazily evaluated and no extra objects are created when evaluating the expression. XmlSlurper is also null-safe: when accessing an attribute that doesn't exist, it returns an empty string; the same goes for a non-existing node.
As a rule of thumb, you want to use XmlSlurper when you intend to process only a small part of the document; while it is more efficient to use XmlParser when you have to process the whole XML.
See also
ff Reading XML using XmlParser ff Searching in XML with GPath
ff http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html
ff http://groovy.codehaus.org/api/groovy/util/slurpersupport/ GPathResult.html
ff http://groovy.codehaus.org/Processing+XML
Reading XML using XmlParser
In the previous recipe, Reading XML using XmlSlurper, we learned how to read an XML document using the XmlSlurper provided by Groovy. Now it's time to look at the other parser available in Groovy, groovy.util.XmlParser. Its internal implementation differs from groovy.util.XmlSlurper, but it exposes a very similar API when it comes to document parsing, navigation, and modification.
In this recipe, we will cover the essential usage scenarios for the XmlParser class and its differences from XmlSlurper.
How to do it...
Let's use the same shakespeare.xml file we used in the Reading XML using XmlSlurper recipe.
1.Reading XML data is very similar to XmlSlurper. You need to create an instance of
XmlParser and pass a file reference to its parse method as shown:
def xmlSource = new File('shakespeare.xml')
def bibliography = new XmlParser().parse(xmlSource)
170
www.it-ebooks.info

Chapter 5
2.As with XmlSlurper, GPath expressions (see the Searching in XML with GPath recipe for more advanced examples) are also possible with XmlParser. For example, the code to print the titles of all plays written after 1592 would be as follows:
println bibliography.'bib:author'.text()
bibliography.'lit:play'
.findAll { it.'lit:year'
.text().toInteger() > 1592 }
.each { println it.'lit:title'.text() }
3.The output of the script will be the same as in the previous recipe:
William Shakespeare Love's Labour's Lost. Romeo and Juliet.
A Midsummer-Night's Dream.
How it works...
Navigating XML data with XmlParser is slightly different from XmlSlurper. In order to find an element, you need to use its fully qualified name (FQN) including the exact prefix. Since, in our XML example, we use the bib: prefix for the author element, we need to refer to author's data as bib:author (or as *:author to be more independent).
If our XML example didn't contain a FQN, then we could have referred to them in a very similar way as we did for XmlSlurper, for example, bibliography.author.
In step 2, you may have noticed that we have used the text method to get the textual representation of the author element. That's because XmlParser returns instances of groovy.util.Node, whose toString method does not return the element's textual content by default. There is also the attribute method that accepts a name and returns the given attribute. If you ask for an attribute that doesn't exist, attribute returns null (this is the opposite behavior of XmlSlurper that returns an empty string).
The main difference between XmlParser and XmlSlurper is that the first uses the groovy.util.Node type and its GPath expressions result in lists of nodes, which are easily manipulable using our knowledge of lists and collections. Compared to XmlSlurper,
XmlParser consumes more memory because it has to create an intermediate data structure to represent the node tree, but it makes XML tree queries a bit faster. So, it's up to developers to decide which implementation better suits their needs.
171
www.it-ebooks.info