XML Parsing with Groovy

By |

Groovy is a powerful, optionally typed and dynamic language and in this post i will show how easily you can parse XML by using a few lines of code. So, let’s do it.

The XML used for this example contains a list of consumers:

def xml = """
        <name>Fábio Santos</name>
        <name>John Doe</name>

Let’s assume that you want to check if a consumer exists by matching an email:

def email_to_match = "fabio@example.com"

So, just parse XML and return true if the consumer’s email exists in the XML:

def consumers = new XmlParser().parseText(xml)

for (consumer in consumers) {
    if (email_to_match.equals(consumer.email.text())) {
        return true

return false