Quantcast
Channel: SCN : Document List - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 571

Flat file to deep XML - using an UDF

$
0
0

'File Content Conversion' OR 'MessageTransformBean' in sender channel can produce a 4 level deep XML, maximum.

A1.png

If expected XML is more than 4 level deep.

A2.png

Solution: - Develop FCC or MTB to read each line of input.

A3.png

Map using an UDF, to split each line to corresponding field.

A4.png

A5.png

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. Other solution available on SCN.

DeepFCCBean - The better FCC at meeting your deep (structure) needs! (Part 2 - Flat File to Deep XML)

XI/PI: Convert Flat File to Deeply Nested XML Structures Using Only Graphical Mapping


Viewing all articles
Browse latest Browse all 571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>