Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
HuseyinSener
Explorer
Introduction

Dear community,

Before I start, I'm happy to write this blog post as it will be my first one. Considering the novice nature of the first blog post, I apologize in advance for any omissions or parts that may not be clear. Due to my curiosity about CPI, I occasionally conduct research and experiments. I would like to share one of these projects with you.

In the this blog, we will learn how to apply for dynamically converting XML into Fixed length file structure.

Fixed Length File Specification


Example XML
<Example>
<xHeaderInfo>
<Header_No>10</Header_No>
<Header_Name>Huseyin</Header_Name>
<Header_Type>H1</Header_Type>
</xHeaderInfo>
<xItemInfo>
<Item_No>01</Item_No>
<Item_Name>Item1</Item_Name>
<Item_Type>I1</Item_Type>
</xItemInfo>
<xFooterInfo>
<Footer_No>01</Footer_No>
<Footer_Name>Footer1</Footer_Name>
</xFooterInfo>
</Example>

Step 1: Create a one-one message map providing the XSD of our xml structure in both source and target for project.

Step 2: Create a custom function in message map using below code.
import com.sap.it.api.mapping.*

def String customFunc(String xResult,String xLength){
int xRequiredLength = 0;
try{
xRequiredLength= Integer.parseInt(xLength);
}
catch (Exception e){}

while (xResult.length() < xRequiredLength){
xResult=xResult+" ";
}
return xResult;

}


Note: First parameter should be the source segment and the second parameter should be the corresponding fixed length value (Constant).

Below is the output of message map
<?xml version="1.0" encoding="UTF-8"?>
<Example>
<xHeaderInfo>
<Header_No>10</Header_No>
<Header_Name>Huseyin</Header_Name>
<Header_Type>H1</Header_Type>
</xHeaderInfo>
<xItemInfo>
<Item_No>01</Item_No>
<Item_Name>Item1</Item_Name>
<Item_Type>I1</Item_Type>
</xItemInfo>
<xFooterInfo>
<Footer_No>01</Footer_No>
<Footer_Name>Footer1</Footer_Name>
</xFooterInfo>
</Example>

Step 3: After the message map step, create a XSLT map using below code.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:output method="text" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="/" mode="serialize">
<xsl:apply-templates select="@*"/>
</xsl:template>
</xsl:stylesheet>>

The above XSLT code, removes the XML tags preserving the values and the white space.

Step 4: The final step. If you have completed all of these smoothly, all you need to do is test. 🙂

Final output fixed length flat file structure.


Conclusion

In this first blog post, We looked at the task of dynamically transforming any XML data to Length Structure File.

Thank you for patiently reading.
2 Comments
Labels in this area