Search code examples
xmlgroovyxmlslurper

extract element value with XmlSlurper using groovy script


I have a input xml like below where I want to extract 'Maker' value and pass it to an element:-

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                ../../Di/xsd/Doc/Data_0100.xsd">
    <Area>
        <CreationDateTime>2020-11-30T15:47:44Z</CreationDateTime>
        <No>
            <Id1>146520459</Id1>
        </No>
    </Area>
    <text>
        <Catalog>
            <Part>
                <Id>12345</Id>
                <Revision/>
                <Mixer>rtg</Mixer>
            </Part>
            <Dis>
                <Maker>7874</Maker>
            </Dis>
        </Catalog>
    </text>
</Data> 

I tried like below, I need to fetch element2 and pass it as property but no luck:-

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.MarkupBuilder;
def Message processData(Message message) {
     def body = message.getBody()
       
       def root = new XmlSlurper().parseText(body);
       
       def element2 = root.text.Catalog.Dis.Maker;
       message.setProperty("element2", element2.text());
       return message;
}

Solution

  • Correction:

     def body = message.getBody(java.lang.String)