O'Reilly logo

Groovy 2 Cookbook by Luciano Fiandesio, Andrey Adamovich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Searching in XML with XPath

XPath is a W3C-standard query language for selecting nodes from an XML document. That is somewhat equivalent to SQL for databases or regular expressions for text. XPath is a very powerful query language, and it's beyond the scope of this book to delve into the extended XPath capabilities. This recipe will show some basic queries to select nodes and groups of nodes.

Getting ready

Let's start as usual by defining an XML document that we can use for selecting nodes:

def todos = ''' <?xml version="1.0" ?> <todos> <task created="2012-09-24" owner="max"> <title>Buy Milk</title> <priority>3</priority> <location>WalMart</location> <due>2012-09-25</due> <alarm-type>sms</alarm-type> <alert-before>1H</alert-before> </task> <task ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required