Skip to Content
0

remove header and footer from sender file

Nov 10, 2017 at 08:41 AM

120

avatar image
Former Member

Hello Experts,

I am working on scenario file to file, but I need to remove header and footer from the file and then send to ECC system. below is the file.

Input file:

START-OF-FILE
RUNDATE=20171016
PROGRAMFLAG=oneshot
FIRMNAME=dlftp
COMPRESS=yes
FILETYPE=pc
REPLYFILENAME=ravi.csv
PROGRAMNAME=getdata


# Automatically generated by ZDemo_File Request Builder version 7.0.0
# Build Code: 010000000100000101


START-OF-FIELDS
PX_LAST
END-OF-FIELDS


TIMESTARTED=Mon Oct 16 09:39:43 EDT 2017
START-OF-DATA
TWDHKD Curncy|0|1|09/27/2017|0.2576|
TWDJPY Curncy|0|1|09/27/2017|3.721|
USDAED Curncy|0|1|09/27/2017|3.6729|
USDAFN Curncy|0|1|09/27/2017|68.2723|
USDAOA Curncy|0|1| | |
USDARS Curncy|0|1|09/27/2017|17.5629|
USDAUD Curncy|0|1|09/27/2017|1.2741|
USDAZN Curncy|0|1|09/27/2017|1.6976|
USDBAM Curncy|0|1|09/27/2017|1.6646|
USDBDT Curncy|0|1|09/27/2017|82.1875|
EURAUD Curncy|0|1|09/27/2017|1.4963|
END-OF-DATA
TIMEFINISHED=Mon Oct 16 09:39:47 EDT 2017
END-OF-FILE


output file:

TWDHKD Curncy|0|1|09/27/2017|0.2576|
TWDJPY Curncy|0|1|09/27/2017|3.721|
USDAED Curncy|0|1|09/27/2017|3.6729|
USDAFN Curncy|0|1|09/27/2017|68.2723|
USDAOA Curncy|0|1| | |
USDARS Curncy|0|1|09/27/2017|17.5629|
USDAUD Curncy|0|1|09/27/2017|1.2741|
USDAZN Curncy|0|1|09/27/2017|1.6976|
USDBAM Curncy|0|1|09/27/2017|1.6646|
USDBDT Curncy|0|1|09/27/2017|82.1875|
EURAUD Curncy|0|1|09/27/2017|1.4963|

please suggest me how can I achieve it using FCC ??

Thanks,

Ravi

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
avatar image
Former Member Nov 10, 2017 at 01:42 PM
3

Hi Ravi;

Can you examine the following java mapping.

package example;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.sap.aii.mapping.api.*;


import java.io.*;
import java.util.Scanner;


public class XMLGenerator extends AbstractTransformation {


	@Override
	public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
			throws StreamTransformationException {


		try {


			InputStream inputstream = transformationInput.getInputPayload().getInputStream();
			OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
			DocumentBuilderFactory document = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = ((DocumentBuilderFactory) document).newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			BufferedReader inpfile = new BufferedReader(new InputStreamReader(inputstream));
			doc.setXmlStandalone(true);
			Element docRoot = doc.createElement("ns0:Example");
			docRoot.setAttribute("xmlns:ns0", "http://example/test");
			doc.appendChild(docRoot);


			String line;
			int i = 0;


			while ((line = inpfile.readLine()) != null)


			{
				if (i >= 21) {
					if (line.contains("END-OF-DATA")) {
						break;
					}


					Scanner rd = new Scanner(line);


					while (rd.hasNext()) {
						Element item = doc.createElement("item");
						docRoot.appendChild(item);


						Element input = doc.createElement("input");
						input.appendChild(doc.createTextNode(line));
						item.appendChild(input);


						break;
					}
				}
				i++;
			}


			TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),
					new StreamResult(outputstream));


		} catch (Exception exception) {
			getTrace().addDebugMessage(exception.getMessage());
			throw new StreamTransformationException(exception.toString());
		}


	}


}



Regards

Hasan


screenshot-1.png (861.1 kB)
Show 7 Share
10 |10000 characters needed characters left characters exceeded

Good java code.

Ravi can enhance the java mapping little further, there are many ways to do it. below one will make it dynamic.

instead of putting if condition if(i>=21)

we ca set the flag true when it finds "START-OF-DATA" and loop, once it finds END-OF-DATA, exit the loop.

1

And furthermore: instead of using memory consuming DOM for just adding the nodes to result document, I would use StringBuilder to compose the string representing result XML.

Regards, Evgeniy.

1

Yeah. easy way to construct xml manually.

or here the expectation is to create flat file, so one can just put lines into StringBuilder, and then into OutputStream. it creates the flat file and can save doing FCC in receiver side.

1

Yes, you're absolutely right. Even FCC isn't required in such case. Just to write output payload directly to target file.

Regards, Evgeniy.

1
Former Member

hello Hasan,

Great work.

But is there any way to do this with File Content Conversion ???

0
Former Member
Former Member

Hi Ravi;

You can refer to the following example.Besides you can use Module configuration.

Mapping;

Receiver File Content Configuration;

Regards
Hasan

mapping.png (556.0 kB)
fcc.png (323.7 kB)
1

if you are looking to achieve using FCC, then read the sender file, line by line, filter the records in the message mapping. construct the output xml and then do the FCC.

https://blogs.sap.com/2005/08/16/configuring-generic-sender-file-cc-adapter/

2
avatar image
Former Member Nov 10, 2017 at 10:56 AM
0

Hi,

You can use Document offset to remove the header. But this will work only when fixed number of lines are there in the header else it may remove some data lines also.

Regards

Jitender

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Jitendra,

I have fixed number of line in the header.

But I need to remove last 3 lines as well.

0