Flat file to deep XML – using an UDF
‘File Content Conversion‘ OR ‘MessageTransformBean‘ in sender channel can produce a 4 level deep XML, maximum.
If expected XML is more than 4 level deep.
Solution: – Develop FCC or MTB to read each line of input.
Map using an UDF, to split each line to corresponding field.
UDF code: – ‘Execution Type’ : ‘All Values of a Context’.
public void udf_Shopping(String[] eachLine, ResultList Customer, ResultList Name, ResultList ID, ResultList Order, ResultList OrderNumber, ResultList OrderNote, ResultList LineItem, ResultList LineNumber, ResultList Material, ResultList TaxLine, ResultList Type, ResultList Amount, Container container) throws StreamTransformationException{
int countCust = 0, countOrdr = 0, countLine = 0;
String values[];
for (String line : eachLine) {
values = line.split(",");
if (line.startsWith("Cust")) {
Customer.addValue("");
Name.addValue(values[1]); Name.addContextChange();
ID.addValue(values[2]); ID.addContextChange();
//Add context change form second Customer.
if (countCust > 0) { Order.addContextChange(); }
countCust++;
} else if (line.startsWith("Ordr")) {
Order.addValue("");
OrderNumber.addValue(values[1]); OrderNumber.addContextChange();
OrderNote.addValue(values[2]); OrderNote.addContextChange();
//Add context change form second Order.
if (countOrdr > 0) { LineItem.addContextChange(); }
countOrdr++;
} else if (line.startsWith("Line")) {
LineItem.addValue("");
LineNumber.addValue(values[1]); LineNumber.addContextChange();
Material.addValue(values[2]); Material.addContextChange();
//Add context change form second LineItem.
if (countLine > 0) { TaxLine.addContextChange(); }
countLine++;
} else if (line.startsWith("Taxl")) {
TaxLine.addValue("");
Type.addValue(values[1]); Type.addContextChange();
Amount.addValue(values[2]); Amount.addContextChange();
}
}
}
Note:-
Comma(delimiter), ‘Cust’, ‘Ordr’, ‘Line’ and ‘Taxl’ arbitrary values are used to demonstrate the concept.
If some fields are optional in input, to avoid ArrayIndexOutOfBoundsException append delimiters.
for (String line : eachLine) {
line = line + “,,,–“; //Append “,,,–” commas for optional fields.
values = line.split(“,”);
….
}
If input is fixed length file (instead of delimited), use java String method substring(int beginIndex, int endIndex).
FYI.
Deep XML to Flat file – using an UDF
Other solution available on SCN.
XSLT approach – Flat File to Deep XMLDeepFCCBean – Flat File to Deep XML
You missed out the following approach based on XSLT (haha... self promo time 😛 )
XSLT approach to enable FCC for deep structures (Part 2 - Flat File to Deep XML)
Anyway, thanks for sharing. Always good to have various options available and leave it to the developer to assess which one is feasible for a particular scenario.