You are on page 1of 2

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

Message;

import groovy.xml.*;

import groovy.xml.XmlUtil;

import java.util.*;

def Message processData(Message message) {

String payload = message.getBody(String);

def xmlRoot = new XmlParser().parseText(payload);


String[] fieldNames =
"PART_NUMBER,PART_NAME,UNIT_PACK_QUANTITY,UNIT_OF_MEASURE,PRICE_EFFECTIVE_DATE,LIST
_PRICE,HAZARDOUS_MATERIAL_INDICATOR".split(",");
String[] fieldLength = "20,40,4,2,6,7,1".split(",");

HashMap<String,Integer> hMap = new HashMap<String,Integer>();

StringBuilder sb = new StringBuilder();

for(int i=0;i<fieldNames.length;i++){

hMap.put(fieldNames[i],Integer.parseInt(fieldLength[i]));

}
xmlRoot.rec.each{

for(int i=0;i<fieldNames.length;i++){

def child = it.find { it.name() == fieldNames[i] };

String childText = "";

if(child != null){

childText = child.text();

sb.append(formatString(childText,hMap.get(fieldNames[i])));

sb.append("\r\n");

};

message.setBody(sb.toString());
return message;

String formatString(String input,int length){

if(input == null){

input = "";

return String.format("%"+length+"s",input).substring(0,length);

You might also like