groovy

Parse XML Iteration node values in Groovy


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:

  1. first validate, if OrderHeaderPartnerType/PartnerFunction equals 'SH', then first collect 'OrderHeaderPartnerType/Customer' value.
  2. Validate if the OrderHeaderPartnerType/Address/PartnerFunction value does not exist, then store the 'Customer' value collected above and store it in the property.

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.


Solution

  • 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() }