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

Deep XML to Flat file - using an UDF

$
0
0

'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


Viewing all articles
Browse latest Browse all 571

Trending Articles



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