Tuesday, December 28, 2010

Handling CSV files

We were interfacing to external vendor who had a CSV restful interface.  I had built a system for creating and parsing the xml files.  Then my co-worker reminded me about the built in flat file handling routines.  Duh!

  1. Create a flat file dictionary with all the different cvs formats you will need.
  2. Then create a flat file schema for each transaction (referencing the object in the dictionary).
  3. Create a document definition from the schema
  4. Take the csv data and send it to pub.flatFile:convertToValues.  Send in the appropriate schema into the ffSchema entry on the service input.  (You can just copy the schema from the navigator and paste it into the value for ffSchema).
  5. map ffValues to a document reference list of the document you created in step 3.
It was easy and straightforward and it handles all the parsing for you.

2 comments:

  1. or
    /*
    Developed by Michael a. Robinson - andremichaels@gmail.com
    Built using basic Java
    */

    String fileName=IDataUtil.getString(pipeline.getCursor(),"File");
    String Source=IDataUtil.getString(pipeline.getCursor(),"Source");
    String Message_Type=IDataUtil.getString(pipeline.getCursor(),"Message_Type");
    StringBuffer xmlstrGlobal = new StringBuffer();

    ArrayList fieldNames = new ArrayList();
    ArrayList fieldvalues = new ArrayList();

    try {
    StringBuffer xmlstr = new StringBuffer();
    xmlstr.append("");
    xmlstr.append(""+fileName+"");
    xmlstr.append(""+Message_Type+"");
    xmlstr.append(""+Source+"");
    xmlstr.append("");
    xmlstr.append("");
    xmlstr.append("");
    xmlstr.append("");
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String strLine = null;
    StringTokenizer st = null;
    int lineNumber = 0, tokenNumber = 0;
    fileName = br.readLine();
    String[] result = fileName.split(",");
    // xmlstr.append(""+result.length+"");

    for (int x = 0; x < result.length; x++) {
    fieldNames.add(result[x]);
    // System.out.print(result[x] +" ");

    }

    while ((fileName = br.readLine()) != null) {
    xmlstr.append("");
    result = fileName.split(",");

    fieldvalues = new ArrayList();
    for (int y = 0; y < result.length; y++) {

    //xmlstr.append("");
    //xmlstr.append(" "+fieldNames.get(y)+"");

    String eval = result[y];
    if (eval == null || eval.equalsIgnoreCase("")
    || eval.equalsIgnoreCase(null)) {
    fieldvalues.add("***");
    } else {
    fieldvalues.add(result[y]);
    }
    xmlstr.append(" <"+fieldNames.get(y).toString().toUpperCase()+">"+fieldvalues.get(y)+"");
    //xmlstr.append(" "+fieldvalues.get(y)+"");
    //xmlstr.append("");

    }
    xmlstr.append("");

    lineNumber++;
    }
    xmlstr.append("");
    xmlstr.append("");
    xmlstr.append("");
    xmlstrGlobal.append(xmlstr);
    xmlstr=null;
    // System.out.println(xmlstr);

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    fileName=null;
    IDataUtil.put(pipeline.getCursor(),"xmlstr", xmlstrGlobal.toString());
    fieldNames = null;
    fieldvalues = null;
    xmlstrGlobal = null;

    ReplyDelete
  2. We have one requirement of converting dynamic IS document to csv excel file

    Do you have any idea for the same.

    Your assistance would be much appreciated.

    ReplyDelete