How to Handle Fixed-Length Files in
SAP CPI
Working with fixed-length files in SAP CPI can be a challenge, especially when performance
and scalability are critical. Unlike SAP PI/PO, which offers File Content Conversion (FCC) for
handling such formats, CPI does not provide a native FCC equivalent. But don't worry—
there’s an effective and scalable approach to solve this!
In this post, we’ll explore:
• Why Groovy isn’t ideal for large volumes
• A performance-friendly alternative
• XSD + CSV-to-XML converter trick
• Message Mapping to extract fixed-length fields
The Challenge
When fixed-length files land in SAP CPI, our first instinct is to parse them using Groovy
scripts. While this works for small payloads, it becomes inefficient when processing
thousands or millions of records—the processing time increases, and memory usage spikes.
Why Not Use Groovy for Heavy Lifting?
Parsing and splitting large fixed-length files line by line using Groovy can slow down your
integration process significantly. Instead of relying entirely on scripting, we can leverage
CPI’s CSV-to-XML converter in a clever way to pre-process the content.
Optimal Approach: Minimal Script + CSV-to-XML Converter
1. Convert the Payload to a String of Records
Instead of parsing each field in the Groovy script, just convert the file into a single string of
records using this simple Groovy code:
import com.sap.gateway.ip.core.customdev.util.Message
import java.io.*
def Message processData(Message message) {
def reader = message.getBody(java.io.Reader)
def writer = new StringWriter()
Confidential C
try {
writer.append("Record\n")
new BufferedReader(reader).each { line ->
writer.append(line + "\n")
}
} finally {
reader.close()
}
message.setBody(writer.toString().trim())
return message
}
2. Define XSD for Conversion
Now import the following XSD schema in your integration flow to define the XML structure:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="row" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Record" minOccurs="0"
maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
3. Use Message Mapping to Split Fields
Once your payload is in XML with multiple <Record> rows, use message mapping to extract
fields using substring logic.
Confidential C
Example:
Field1: substring(Record, 0, 10)
Field2: substring(Record, 10, 20)
...
Benefits
• Improved performance and faster processing
• Minimal Groovy scripting
• Reusable mapping logic
• Scalable for large files
Summary
Handling fixed-length files in SAP CPI may lack a native FCC feature, but with a smart
combination of lightweight Groovy + XSD + Message Mapping, we can build scalable and
performant integration flows.
Confidential C