Fork me on GitHub

Programming, Internet & more

Parse XML files with Groovy

Have you ever wondered how to parse a XML document with Groovy?

Assume that we have the following xml stored in the file notes.xml:

        <body>Don't forget me this weekend!</body>
        <heading>Grocery list</heading>
        <body>Milk and Cheese</body>

Parsing this document with Groovy is really simple. To retrieve the note from Dave we can use the following code:

// parse
def xml = new XmlSlurper().parse("notes.xml")

// find nodes by from
def nodes = xml.depthFirst().find { == 'note' && it.getProperty('from') == 'Dave'

// print results

That’s it. It’s just that simple.

Category: programming

Leave a Reply

Your email address will not be published. Required fields are marked *