Skip to Content
Author's profile photo Raghu Vamseedhar Reddy Kadipi Reddy

Deep XML to Flat file – using an UDF

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;
  String elem;
  while (m.find()) { //Loop over elements.
  elem =;
  if (elem.startsWith("<Tag>") && count > 0) { //From second Tag
  //Get text content of element. E.g: <Name>AAA</Name> = AAA.
  element.addValue(elem.substring(elem.indexOf(">") + 1, elem.indexOf("</")));

Create receiver FCC or MTB.



‘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 =, – 2);

    in[0] = in[0].replace(, “<” + elementName + “></” + elementName + “>”);



Flat file to deep XML – using an UDF

Copy XML Subtree

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

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.