Skip to Content

HCI: Parse XML easily with Groovy scripts


HCI allows the usage of scripting languages in Integration Flows which can be used to enhance the functionality of the integration flow. Groovy scripts or Javascript can be implemented as a Message Transformer block or as a User Defined Function for Message Mappings.

Groovy is a dynamic language with native functionality to handle XML processing easily. Compared to Java, implementing logic for XML processing is really simple in Groovy, it is not as complex as DOM and have a low memory footprint as it is based on SAX.

In this blog, I will share some simple examples of how XML can be parsed easily in Groovy.


The examples below will all use the following input XML payload.

Input Payload

<?xml version='1.0' encoding='UTF-8'?>

Scenario 1 – Read-only access to input XML

For read-only access to the input XML, it is recommended to use groovy.util.XmlSlurper. XmlSlurper contains a parseText() method which returns a GPathResult for the root element. Using this GPathResult object, accessing the content of the document can easily be done in an XPath-like manner. As shown in the logic below, the text contents of /Records/Line/Field1 and /Records/Line/Field4 are accessed using dot notation without any further declarations or DOM-like complex logic.


  public void parse(String input) {
    def root = new XmlSlurper().parseText(input);
    def field1 = root.Line.Field1.text();
    def field4 = root.Line.Field4.text();
    System.out.println("Value of /Records/Line/Field1 = " + field1);
    System.out.println("Value of /Records/Line/Field4 = " + field4);

Below is the console output for the above example verifying the successful extraction of the two field contents.


Scenario 2 – Change content of a field in input XML

If the input XML needs to be updated/changed during parsing, it is recommended to use groovy.util.XmlParser instead. Similar to above, it contains a parseText() method but returns a Node for the root element. Similarly, using GPath dot notation, content of the field can be directly modified as shown below for /Records/Line/Field1.


  public static void change(String input) {
    def root = new XmlParser().parseText(input);
    root.Line.Field1[0].value = "MyChangedField";

Below is the console output verifying the successful modification of the field.


Scenario 3 – Add a new field in input XML

Similar to scenario 2, XmlParser is used to parse the input XML. In order to add a new field, the appendNode() method is used to add a new field and initialize the text content in it.


  public static void add(String input) {
    def root = new XmlParser().parseText(input);
    root.Line[0].appendNode("Field5", [:], "MyNewField");

Below is the console output verifying the successful addition of a new field in the XML payload.



As demonstrated from the examples above, parsing of XML content is significantly simpler to implement in Groovy with the use of XmlParser and XmlSlurper.


The Groovy programming language – Processing XML

You must be Logged on to comment or reply to a post.
  • Hello,

    Are you aware of any modes to debug the Groovy Script in HCI during the runtime?

    I have added System.out.println statements but not sure which console this gets printed to!



    • Hi Bhavesh

      Unfortunately I don’t. I haven’t come across any documentation on that (in whatever little we have of HCI!).

      A long windy way that I used was to store values in property and route the message to the following HTTP logging server. Property values in HCI will be shown as HTTP property on the server’s logs.

      Testing: Test Tools…Part 1 *HTTP *


      Eng Swee

      • Hello Eng Swee,

        Thanks for your feedback and hint on using the HTTP Test Tool.

        Meanwhile, I was able to figure this out. The HCI Developer’s Guide has this info.

        The MessageLog is accessible using the below piece of code. Was thereby able to add some Trace Entries and see what’s happening within my Groovy Script.

        def messageLog = messageLogFactory.getMessageLog(message) messageLog.setStringProperty(“Greeting”, “Hello World!”)



        • Nice one, Bhavesh.

          I had a look at the online documentation and it looks like it has been updated since I used it during my trial access. 🙂

          • The Fun and the Bane of Cloud, Change faster than one can comprehend 😏

            I am glad I could figure this out and thanks for all the HCI Blogs.. I hope to share my part of the learnings sometime soon!

  • Hi Eng Swee,

    Our use case is to convert the output XML message (from a receiver) from one format to another via scripts in HCI.

    Do you have any recommendations to do the same ?

    Can we use XSLT here?

    • Hi Lavanya

      XSLT is commonly used to transform XML documents to other XML documents, or even HTML or plain text. You can use XSLT if the input & output formats are as mentioned. If they are not, then you will have to go for Groovy scripts.


      Eng Swee

  • Great Post..!
    I am not able to access my .xml file that i have kept in src.main.resources folder.
    I am trying to access it from my script.gsh which is kept in src.main.resources.script folder.

    Please let me know how can i access my .xml file.

    Thanks in advance

  • Hi Eng,

    The post is really helpful to understand how to use groovy in HCI , however i have a requirement to replace a attribute in xml, can you please let me know how to achieve it? attribute lang should be changed as xml:lang



      <?xml version="1.0" encoding="UTF-8"?>
        <Description lang="en">TEST</Description>
        <Period endDate="2016-09-01T23:59:59+02:00" startDate="2016-09-01T00:00:00+02:00"/>
        <ForecastQuantity quantity="34.0">
        <name lang="en">TEST1</name>
      <?xml version="1.0" encoding="UTF-8"?>
        <Description xml:lang="en">TEST</Description>
        <Period endDate="2016-09-01T23:59:59+02:00" startDate="2016-09-01T00:00:00+02:00"/>
        <ForecastQuantity quantity="34.0">
        <name xml:lang="en">TEST1</name>
  • Hi Eng,

    I need to create multiple nodes in a HCI mapping. I want to do this with Groovy script, the same way you can do this in PI in a UDF (returning an array via results.addvalue), but I can’t get it to work. Do  you have any experience with this?





  • Hi Eng Swee Yeoh,

    I have installed the Groovy plug in for Eclipse.  Is there a way to simply run the Groovy scripts developed in HCI through Eclipse easily?  Is there a jar I can add to the build path so the following import works?


    In our use case, we have a very complex Groovy script in HCI.  We would like to test just this map by itself in Eclipse by passing in the message and test our logic without running the entire flow.






  • Hi Eng Swee,


    I’m a bit at a loss on how to incorporate this in the standard groovy script.

    I assume I need to get the message body first, so use the

    ‘def Message processData(Message message) { ‘  statement first.


    But after that I am no longer allowed to use the Public Void statement.

    So how do I incorporate this in a normal CPI grooy script?




  • I succeeded after only umpteen tries and on the route really learned what a node object was..


    import groovy.json.*;
    import groovy.util.*;
    import groovy.xml.*;
    import org.codehaus.*;
    import java.util.HashMap;
    def Message processData(Message message) {
    	def pMap = message.getProperties();
    	def Period_Start_Date_ext = pMap.get("Date_d_execution_depuis_le");
    	def Period_End_Date_ext = pMap.get("Date_d_execution_jusquau");
        def body_xml= message.getBody(java.lang.String) as String;
    	Node root = new XmlParser().parseText(body_xml);
        root.appendNode("period_start_date", [:], Period_Start_Date_ext);
    	root.appendNode("period_end_date", [:], Period_End_Date_ext);
    	def xml = XmlUtil.serialize(root);
    return message;
    • Hi Mary


      You can download the CP Default Trace from System Log Files and view it there, but it doesn’t have the nicest view.



      Eng Swee