Skip to Content

File Content Conversion‘ OR ‘MessageTransformBean‘ in receiver channel can process 3 level deep XML, as input.

A1.png

If input XML to receiver channel is more than 3 level deep.

A2.png

Solution:- Map using an UDF.

A3.png

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.

A4.png

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

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

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply