There is no option in the CVS to XML convertor in CPI for converting the flat file. We can customize using the groovy script to achieve this.
Input:
NHEDSAPPS220C16 20190523045
16 20190523046
16 20190523047
16 20190523048
TRAR000712
Groovy Script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def lines = body.split("\n");
// variable initialization
def lineArr;
def itemtypPre;
def irlflag="E";
def irflag="E";
def hflag="E";
def Fieldentry = "";
def itemtyp;
def i=0;
//XML and root element formation
Fieldentry += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Fieldentry += "<Root>";
for(i=0;i<lines.length;i++)
{
if(i>0)
{
//Item Type holder split from File i.e NH, 16, TR
itemtypPre = lines[i-1].substring(0,1);
}
//Item Type holder split from File i.e NH, 16, TR
itemtyp= lines[i].substring(0,2);
if(itemtyp.equals("NH"))
{
Fieldentry += "<Header>";
Fieldentry += "<ItemType>"+lines[i].substring(0,3)+"</ItemType>";
Fieldentry += "<ISPID>"+lines[i].substring(4,5)+"</ISPID>";
Fieldentry += "<Rate>"+lines[i].substring(6,7)+"</Rate>"
Fieldentry += "</Header>";
}
if(itemtyp.equals("16")){
if(irlflag.equals("O"))
{
Fieldentry += "</Items>";
irlflag="E";
}
Fieldentry += "<Items>";
irlflag="O";
// Fieldentry += "<ItemType>";
Fieldentry += "<Type>"+lines[i].substring(0,2)+"</Type>"
Fieldentry += "<FullClaimNumber>"+lines[i].substring(3,5)+"</FullClaimNumber>"
Fieldentry += "<ClaimNumber>"+lines[i].substring(4,12)+"</ClaimNumber>"
Fieldentry += "<AuthorizationCode>"+lines[i].substring(13,15)+"</AuthorizationCode>"
// Fieldentry += "</ItemType>";
}
if(itemtyp.equals("TR")){
Fieldentry += "</Items><Trailer>";
Fieldentry += "<Type>"+lines[i].substring(0,4)+"</Type>"
Fieldentry += "<NumExtendedWarranty>"+lines[i].substring(5,8)+"</NumExtendedWarranty>"
Fieldentry += "<ElectroluxCustomerId>"+lines[i].substring(9,10)+"</ElectroluxCustomerId>"
Fieldentry += "</Trailer>";
} }
Fieldentry += "</Root>";
message.setBody(Fieldentry);
return message;
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Header>
<ItemType>NHE</ItemType>
<ISPID>S</ISPID>
<Rate>P</Rate>
</Header>
<Items>
<Type>16</Type>
<FullClaimNumber> 2</FullClaimNumber>
<ClaimNumber>20190523</ClaimNumber>
<AuthorizationCode>45</AuthorizationCode>
</Items>
<Items>
<Type>16</Type>
<FullClaimNumber> 2</FullClaimNumber>
<ClaimNumber>20190523</ClaimNumber>
<AuthorizationCode>46</AuthorizationCode>
</Items>
<Items>
<Type>16</Type>
<FullClaimNumber> 2</FullClaimNumber>
<ClaimNumber>20190523</ClaimNumber>
<AuthorizationCode>47</AuthorizationCode>
</Items>
<Items>
<Type>16</Type>
<FullClaimNumber> 2</FullClaimNumber>
<ClaimNumber>20190523</ClaimNumber>
<AuthorizationCode>48</AuthorizationCode>
</Items>
<Trailer>
<Type>TRAR</Type>
<NumExtendedWarranty>007</NumExtendedWarranty>
<ElectroluxCustomerId>2</ElectroluxCustomerId>
</Trailer>
</Root>
How can we pass field parameters using content modifier, Can you pls let me know
ReplyDelete