Monday, March 7, 2022

Content Modifier - Concatenate Array Inputs ( Dynamic Parent Node)

 

 Requirement is to extract the repeated XML node with Dynamic Parent Nodes.






Here, we are passing the below payload directly in Content Modifier - Message Body

--------------------------------------------------------------------------------------------------------------------

<message>

    <header>

      <messageId>1</messageId>

      <masterMsgId>33300453700</masterMsgId>

      <equipment>

        <serialNumber>API TestSNVin</serialNumber>

        <make>CAT</make>

        <model>D100</model>

        <nickname>API Test Asset</nickname>

      </equipment>

      <moduleCode>API TestDeviceSN</moduleCode>

      <moduleTime>2022-02-16 10:45:14</moduleTime>

      <receivedTime>2022-02-16 10:45:14</receivedTime>

      <owner>Equipment Owner</owner>

    </header>

    <serviceMeter>

      <value>79</value>

      <uom>Hours</uom>

    </serviceMeter>

    <mileage>

      <value>13</value>

      <uom>Miles</uom>

    </mileage>

    <location>

      <latitude>7</latitude>

      <longitude>77</longitude>

      <validity>V</validity>

    </location>

  </message>

--------------------------------------------------------------------------------------------------------------------


Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;


import java.util.HashMap;

import java.util.*;

def Message processData(Message message)

{

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

    def root = new XmlParser().parseText(body);

    def i=0;

    def errors="";

    def allmessage=[];

        root.'**'.findAll { it.name() == 'value'}.each { a ->allmessage << a.text()};

    int len = allmessage.size();

    while(i<allmessage.size())

    {

        errors=errors+","+allmessage[i];

        i++;

    }

        errors=errors.substring(1);

     def messageLog = messageLogFactory.getMessageLog(message);

       if(messageLog != null){

        messageLog.setStringProperty("Logging1", "Printing Payload As Attachment");

        messageLog.addAttachmentAsString("only_message:", len + "message – "+errors, "text/plain");

     }

     message.setBody(errors);

    return message;

}




--------------------------------------------------------------------------------------------------------------------

Output:  We need the data from Value nodes

<value></value>  ,  with two Parent nodes (Dynamic) -  serviceMeter and mileage





No comments:

Post a Comment