Skip to Content

If you want to read a CSV file using the file adapter, file content conversion is the first option which strikes your mind.

If you are comfortable with java coding, here i present you with an alternate solution.

I will show you how to use java mapping to achieve the same.

Below is the content in the csv file which i want to read.

1,rahul,siemens,mumbai

2,consultant,12032005

1,viswanath,sisl,hyderabad

2,systemeng,23052005

‘1’ is used to identify the header level record and ‘2’ the item level.

Now you have to write the java mapping program which will read this data and create a target xml structure

package TXTMapping;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.Map;

import com.sap.aii.mapping.api.StreamTransformation;

public class TMapping implements StreamTransformation {

private Map map;

       

public void setParameter (Map param){

      map = param;

        }

public void execute (InputStream in, OutputStream out){

  try{

    out.write(“”Name“”Company“”Place“”Desgn“”Since“.xls or .pdf or .txt) and do the conversion in the mapping program.

To report this post you need to login first.

17 Comments

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

  1. Anton Wenzelhuemer
    this very example is part of almost any course of ‘Java for Absolute Beginners’ and done much more elegant there, e.g. using a method

    insertNode(nodename, nodecontent)

    etc.

    anton

    (0) 
  2. Anonymous
    Well, I would recommend not to use that kind of code in teaching XML.

    In particular, it’s a very bad idea to first claim that the output is UTF-8, and then to use String.getBytes() to produce it (because that one will use whatever the system encoding happens to be).

    Best regards, Julian

    (0) 
    1. Rahul Nawale Post author
      Julian…
      You are correct..i am not a java developer and the code written is just a sample example…i just wanted to highlight that it can be done using java mapping

      Regards
      Rahul Nawale

      (0) 
  3. Michal Krawczyk
    hi Rahul,

    why do you want to read it in a mapping?
    you can do it via adapter module (in file adapter)
    or via java proxy if you really need to use
    java to read a flat file

    why did you choose to do it in a mapping?

    Regards,
    michal

    (0) 
    1. Rahul Nawale Post author
      yes michal…we can do it using adapter module and java proxies, no doubt about it. Creating a java class is simpler for the people who dont know ejb development.
      (0) 
  4. Valery Silaev
    package TXTMapping, class TMapping…

    Rahul, seems that you are from Borland Delphi camp 😉 Java has other naming conventions…

    Valery

    (0) 
  5. Piyush Srivastava
    Hi rahul Good Work that, but i am not sure why you want to use Java Maps for reading the files, Adapter modules could be doing this No??

    Any way good simple explanatory Blog.., Good attempt,

    I had a same case with receiver adapter needed to send the CSV file i used Java Code and attached that to the module.

    Any way

    Thanks
    Piyush
    🙂

    (0) 
  6. Anonymous
    Hey Rahul,

    Yes looks like a good one for Beginers.
    One mite just go in for java than Ejbs..

    Regards,
    Vishal

    (0) 
  7. Anonymous
    Hey Rahul,

    Yes looks like a good one for Beginers.
    One mite just go in for java than Ejbs..

    Regards,
    Vishal

    (0) 
  8. Prasad Ulagappan
    Hi Rahul,

    I dont think that it is a good idea to go for java mapping for just reading a file, because, u have taken a very simple file structure to do this. If the structure becomes complex, then Content conversion is easier than this (if u want to do mapping after that).

    Also, if v need to change the target structure, then v need to another mapping for the same which will definelty affect the performance of the scenario.

    Regards,
    Prasad U

    (0) 
  9. Mark Whorton
    I’m trying to put this together and not sure of the setting in the design area to enable this to work.  Can you provide any details.
    (0) 
  10. Bharath Sai
    Hi Rahul,

    Since i can see many explaining that content conversion can be done in the channel instead of in the mapping, you can also mention, for a better clarity, in your blog, that in cases where HTTP (or any channel for that matter where content conversion is not possible) is the sender channel and the data from the sender application is ‘comma delimited’ then we will be left with no choice than to do the content conversion using a java mapping as shown in your blog.

    And also cant we use a string tokenizer in your code, is there any particular reason you hav chosen a char array.. jst curious 🙂

    (0) 
  11. Anupam Ghosh
    Hello Rahul,

    First of all thanks a zillion for this wonderful blog. We are trying to follow your instructions to read from a binary file. I quote a text from your post “Compile the above java mapping code. Zip the class file which is created and import it in the imported archive. Now while doing the interface mapping, specify the type of the mapping program as java class and the imported archieve as the mapping program.”
    In case of interface mapping we need a source xml structure and a target xml structure. The target xml structure is being generated by your java mapping code, but how and what to specify in the source xml structure. Since we are not using FCC,how are we going to get the source xml structure.Could please kindly help us in this regard.

    regards Anupam

    (0) 

Leave a Reply