---
title: How to use Groovy to return values in  XML strings
description: Cycle includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts. 
---

[Skip to content](https://kb.cyclelabs.io/how-to-use-groovy-to-return-values-in-xml-strings#main-content)

English - United States

Show submenu for translations

![](https://kb.cyclelabs.io/hs-fs/hubfs/main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png?width=500&height=93&name=main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png)

Open main navigation

Close main navigation

- English - United States
  
  Show submenu for translations
- [Get Support & Track Tickets](https://help.cyclelabs.io/)

[Get Support & Track Tickets](https://help.cyclelabs.io/?hsLang=en-us)

 How can we help you?

- There are no suggestions because the search field is empty.

1. [Cycle Knowledge Base](https://kb.cyclelabs.io/?hsLang=en-us)
2. [Tips and Tricks](https://kb.cyclelabs.io/tips-and-tricks?hsLang=en-us)

# How to use Groovy to return values in XML strings

## Cycle includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts.

The two Cycle steps that execute Groovy are: 

I execute Groovy " "   
I execute Groovy script " "

**This article assumes a conceptual familiarity with Groovy/Java including general syntax, data types, import statements, class references and method usage.**

For more information on Groovy see [Apache Groovy](http://groovy-lang.org/)

Two items of note regarding Cycle and Groovy.

- 1\. Cycle can only input and output **String** and **Number** data types with Groovy statements and scripts. 
    - Dates, arrays, lists, maps are not yet functional inputs or outputs but can be used within a Groovy script.
- 2\. Cycle outputs a variable called **$groovy\_result** which represents the final result 
    - Custom variables can be assigned and used

 Examples Return value in first occurrence of an XML tag

This Groovy script, **returnFirst.groovy**, imports the necessary Classes, parses an XML string value, uses the node attributes to find and print the output in Notepad (just for visual validation).

The values passed in from Cycle are xml - the xml string and tag - the XML tag to search by.

```
import java.util.*import groovy.xml.*//Parse the input string to XMLdef stringXML = new XmlParser().parseText(xml)//Use XML Node attributes to find first occurrence of a tagval = stringXML.depthFirst().find{it -> it.name() == tag}.text()output = val.toString()
```

This is an example of a Scenario calling **returnFirst.groovy**. The expected output will be only the first occurrence of the tag **WH\_ID** which is **WMD1**.

```
Scenario: Return first value for tagGiven I assign "

   WMD1

   CLT1

    WMD1

    ORD1

    1

    WIDGET

 " to variable "xml"  And I assign "WH_ID" to variable "tag"When I execute groovy script "scripts\returnFirst.groovy"  Then I execute process "notepad.exe"  And I wait 1 seconds  And I enter $output
```

 

 Return value in X occurrence of an XML tag

To go from return first to specifying which node's value to return requires a small change to the original Groovy script.

The code below replaces the bold lines in the original Groovy script

```
//Use XML Node attributes to return value by tag and nodeval = stringXML.depthFirst().findAll{it -> it.name() == tag}[node-1].text()
```

The Features simply requires the addition of setting a node variable:

```
And I assign 2 to variable "node"
```

 

 

- [Getting started](https://kb.cyclelabs.io/getting-started?hsLang=en-us)
- [Using Cycle with BYWMS](https://kb.cyclelabs.io/using-cycle-with-bywms?hsLang=en-us)
- [Cycle Appliance and CI/CD](https://kb.cyclelabs.io/cycle-appliance-and-ci-cd?hsLang=en-us)
- [Tips and Tricks](https://kb.cyclelabs.io/tips-and-tricks?hsLang=en-us)
- [Native App](https://kb.cyclelabs.io/native-app?hsLang=en-us)
- [Deprecated Steps](https://kb.cyclelabs.io/deprecated-steps?hsLang=en-us)
- [Common Issues](https://kb.cyclelabs.io/common-issues?hsLang=en-us)

[![](https://kb.cyclelabs.io/hs-fs/hubfs/main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png?width=500&height=93&name=main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png)](http://cyclelabs.io)

<https://www.facebook.com/> <https://www.twitter.com/> <https://www.instagram.com/> <https://podcasts.apple.com/> [mailto:email@email.com](mailto:email@email.com)

Copyright © 2026, Cycle Labs