I would like to parse the XML structure which contains iteration nodes, In the end, I need to collect the respective values by validating the data in the XML.
The 'OrderHeaderPartnerType' node comes multiple times in the XML, and I need to validate with the below steps:
my XML Input:
<Order>
<OrderType>
<CreationDate>2024-10-21T00:00:00.000</CreationDate>
<Partner>
<OrderHeaderPartnerType>
<PartnerFunction>SP</PartnerFunction>
<Customer>1000082984</Customer>
<Address/>
</OrderHeaderPartnerType>
<OrderHeaderPartnerType>
<PartnerFunction>SH</PartnerFunction>
<Customer>1000082984</Customer>
<Address>
<OrderPartnerAddressType>
<StreetName>Adresse: husplan 45/47</StreetName>
<PartnerFunction>SH</PartnerFunction>
</OrderPartnerAddressType>
</Address>
</OrderHeaderPartnerType>
</Partner>
</OrderType>
</Order>
Groovy Script
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.util.XmlSlurper;
import groovy.xml.XmlUtil;
import groovy.json.*;
def Message processData(Message message){
//read input XML message and stored in property
def body = message.getBody(String);
def data = new XmlSlurper().parseText(body)
// set the input body to the parameter
message.setBody(body)
message.setProperty("p_readInput", body)
// Identify OrderHeaderPartner PartnerFunction value is equal to 'SH'
data.OrderType.Partner.OrderHeaderPartnerType.children().each{OrderHeaderPartnerType ->
def OrdHdrPartnerFunctionValue = ${OrderHeaderPartnerType.PartnerFunction()}
// collect SalesOrderHeaderPartner 'Customer' value
def OrdHdrPartnerFunctionCustomerValue = ${OrderHeaderPartnerType.Customer()}
if (OrdHdrPartnerFunctionValue == SH)
{
// Identify OrderHeaderPartner Address PartnerFunction value exists in order to know whole address details are exists'
data.OrderType.Partner.OrderHeaderPartnerType.Address.OrderPartnerAddressType.children().each{ addtag ->
def HdrPartnerFunction_AddressValue = ${addtag.PartnerFunction()}
if (HdrPartnerFunction_AddressValue == null)
{
message.setProperty("p_HdrPartnerFunctionCustomerValue",OrdHdrPartnerFunctionCustomerValue)
}
}}}
return message
}
Expected output result
If based upon above above-described conditions are met, then 'OrderHeaderPartnerType/Customer' value to the properties along with the Input body.
import groovy.xml.*;
def body = '''
<Order>
<OrderType>
<CreationDate>2024-10-21T00:00:00.000</CreationDate>
<Partner>
<OrderHeaderPartnerType>
<PartnerFunction>SP</PartnerFunction>
<Customer>1000082984</Customer>
<Address/>
</OrderHeaderPartnerType>
<OrderHeaderPartnerType>
<PartnerFunction>SH</PartnerFunction>
<Customer>1000082984</Customer>
<Address>
<OrderPartnerAddressType>
<StreetName>Adresse: husplan 45/47</StreetName>
<PartnerFunction>SH</PartnerFunction>
</OrderPartnerAddressType>
</Address>
</OrderHeaderPartnerType>
</Partner>
</OrderType>
</Order>
'''
def data = new XmlSlurper().parseText(body)
var customerList = data.OrderType.Partner.OrderHeaderPartnerType
.findAll{ it.PartnerFunction=='SH' && it.Address.PartnerFunction.size()==0 }
.collect{ it.Customer.text() }