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...