'File Content Conversion' OR 'MessageTransformBean' in receiver channel can process 3 level deep XML, as input.
If input XML to receiver channel is more than 3 level deep.
Solution:- Map using an UDF.
Create an UDF with ‘Execution Type’ : ‘All Values of a Context’.
public void udf_DeepToFlat(String[] in, ResultList eachLine, ResultList element, Container container) throws StreamTransformationException{ //Identifiers are added. E.g: Cust, Ordr, Line, Taxl. in[0] = in[0].replace("<Customer>", "<Tag>Cust</Tag>").replace("<Order>", "<Tag>Ordr</Tag>").replace("<LineItem>", "<Tag>Line</Tag>").replace("<TaxLine>", "<Tag>Taxl</Tag>"); //Match each element. E.g: <Name>AAA</Name>, <ID>123</ID>. java.util.regex.Matcher m = java.util.regex.Pattern.compile("<(.*?)>[^<]*</\\1>").matcher(in[0]); int count = 0; eachLine.addValue(""); String elem; while (m.find()) { //Loop over elements. elem = m.group(); if (elem.startsWith("<Tag>") && count > 0) { //From second Tag eachLine.addValue(""); element.addContextChange(); } //Get text content of element. E.g: <Name>AAA</Name> = AAA. element.addValue(elem.substring(elem.indexOf(">") + 1, elem.indexOf("</"))); count++; } }
Create receiver FCC or MTB.
Note:-
'Cust', 'Ordr', 'Line' and 'Taxl' arbitrary values are used to demonstrate the concept.
If some elements are represented in short form i.e., <Name></Name> is represented <Name/>. Add below code before element match, to replace short form with long form representation.
java.util.regex.Matcher m1 = java.util.regex.Pattern.compile("<[^<]*?/>").matcher(in[0]);
while (m1.find()) {
String elementName = m1.group().substring(1, m1.group().length() - 2);
in[0] = in[0].replace(m1.group(), "<" + elementName + "></" + elementName + ">");
}
FYI.
Flat file to deep XML - using an UDF
Choosing XML parser, 'Choosing Your Model' When to Use DOM .
Other solutions available on SCN.
File Conversion using 'Nodeception',
XSLT approach - Deep XML to Flat File, DeepFCCBean - Deep XML to Flat File