Jump to content

CSV: Difference between revisions

From Commander4j
Updated by push_wiki.py
 
Line 1: Line 1:
[[Category: Middleware4j]]
The CSV [[Connectors|connector]] reads and writes text files in which the fields are separated by a character such as a comma. It can be used for input and output.
 
=CSV Map Configuration=
=CSV Map Configuration=
'''config.xml'''
'''config.xml'''
Line 56: Line 57:
</syntaxhighlight>
</syntaxhighlight>


In the <csvOptions>separator=,^quote="</csvOptions> element you will see that 2 values are set, '''separator''' and '''quote'''. These 2 settings are separated by a delimiter which happens to be **^** in this example. However if by some unfortunate coincedence your CSV file uses that character as a value separater then you might need to use a different delimited in the configuration - which is where the `<optionDelimeter>^</optionDelimeter>` can be used.
In the <code>&lt;csvOptions&gt;</code> element you will see that 2 values are set, '''separator''' and '''quote'''. These 2 settings are separated by a delimiter which happens to be '''^''' in this example. However if by some unfortunate coincidence your CSV file uses that character as a value separator then you need to use a different delimiter in the configuration, which is what <code>&lt;optionDelimeter&gt;</code> is for.
 
Both <code>&lt;optionDelimeter&gt;</code> and <code>&lt;csvOptions&gt;</code> must be present on a CSV input connector.


[[file:delimiter.png|link=|400px]]
[[file:delimiter.png|link=|400px]]
Line 65: Line 68:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
  <csvOptions>separator=,^quote=none</csvOptions>
  <csvOptions>separator=,^quote=none</csvOptions>
</syntaxhighlight>
=CSV Options=
{| class="wikitable"
|-
! Option !! Used by !! Description
|-
| separator || Input and output || The single character between fields, for example <code>separator=,</code> or <code>separator=;</code>.
|-
| quote || Input || The character placed around a field that contains the separator, normally <code>quote="</code>. Use <code>quote=none</code> if the file does not use quotes.
|-
| quote || Output || <code>quote=none</code> writes the values with no quotes. Any other value puts double quotes around every field.
|-
| endofline || Output || Leave out for lines ending in a line feed. <code>endofline=crlf</code> ends each line with carriage return and line feed, as Windows programs usually expect.
|}
An example with all three, using ^ as the delimiter between options:
<syntaxhighlight lang="xml">
<csvOptions>separator=;^quote=none^endofline=crlf</csvOptions>
</syntaxhighlight>
</syntaxhighlight>


Line 123: Line 146:
| csvOptions || var=val || See earlier description.
| csvOptions || var=val || See earlier description.
|-
|-
| outputFileExtension || Blank or ext || File extension to use for output file.
| fileExtension || Blank or ext || File extension to use for output file. Blank gives <code>csv</code>.
|-
|-
| prefix || String || Option to force all output files to begin with a specific string prefix
| prefix || String || Option to force all output files to begin with a specific string prefix
|-
|-
| use83GUID || Y or N || For backwards compatibility with legacy apps which don't support long filenames. Generates a filename in 8.3 format
| use83GUID || true or false || For backwards compatibility with legacy apps which don't support long filenames. Generates a filename in 8.3 format
|-
|-
| XSLT || filename || Not used
| XSLT || filename || Optional style sheet for this output. Not used in this example, where the style sheet is on the input.
|}
|}
=What the CSV Output Connector Expects=
The style sheet must produce a <code>&lt;data&gt;</code> element containing <code>&lt;row&gt;</code> elements, and '''every row must have a <code>cols</code> attribute''' saying how many fields to write. This is the XML produced by <code>Map12_XML_to_CSV.xsl</code> in the example above:
<syntaxhighlight lang="xml">
<data type="CSV">
    <row id="1" cols="13">
      <col id="1">P/20725187</col>
      <col id="2">43170672</col>
      <col id="3">Reverse Ratcheting Router</col>
      ...
    </row>
</data>
</syntaxhighlight>
Writing stops at the first row without a <code>cols</code> attribute, so if the attribute is missing the output file is empty. The XML made by the CSV '''input''' connector, shown further down this page, has no <code>cols</code> attribute on its rows. To write a CSV file from a CSV file, for example to change the separator or drop some columns, use a style sheet that copies the rows and adds the attribute.


=CSV Example Input=
=CSV Example Input=
Line 218: Line 257:
  </test>
  </test>
</syntaxhighlight>
</syntaxhighlight>
See also: [[Connectors]], [[Maps]], [[Creating a Map]]
[[Category:Middleware4j]]

Latest revision as of 21:47, 9 November 2026

The CSV connector reads and writes text files in which the fields are separated by a character such as a comma. It can be used for input and output.

CSV Map Configuration

config.xml

      <map>
         <id>Map04</id>
         <enabled>true</enabled>
         <description>CSV to XML</description>
         <enableEmailNotifications>true</enableEmailNotifications>
         <connectors>
            <input>
               <id>in1</id>
               <description>Read CSV</description>
               <type>CSV</type>
               <url>
                  <path>./interface/input/Map04 CSV to XML</path>
                  <mask/>
                  <prefix/>
                  <pollingInterval>1000</pollingInterval>
               </url>
               <csv>
                  <optionDelimeter>^</optionDelimeter>
                  <csvOptions>separator=,^quote=none</csvOptions>
               </csv>
            </input>
            <output>
               <id>out1</id>
               <enabled>true</enabled>
               <description>Write XML</description>
               <type>XML</type>
               <url>
                  <path>./interface/output/Map04 CSV to XML</path>
                  <prefix/>
                  <fileExtension/>
                  <use83GUID>false</use83GUID>
               </url>
               <xsl>
                  <XSLT>Map04_CSV_to_XML.xsl</XSLT>
                  <XSLTPath/>
               </xsl>
            </output>
         </connectors>
      </map>

The CSV input connector uses the OpenCSV library and requires very little configuration. The are a pair of settings which you should review

 <optionDelimeter>^</optionDelimeter>

and

 <csvOptions>separator=,^quote="</csvOptions>

In the <csvOptions> element you will see that 2 values are set, separator and quote. These 2 settings are separated by a delimiter which happens to be ^ in this example. However if by some unfortunate coincidence your CSV file uses that character as a value separator then you need to use a different delimiter in the configuration, which is what <optionDelimeter> is for.

Both <optionDelimeter> and <csvOptions> must be present on a CSV input connector.

NOTE that if you don't want a quote character this should be represented as shown below with _none_ used as the value.

 <csvOptions>separator=,^quote=none</csvOptions>

CSV Options

Option Used by Description
separator Input and output The single character between fields, for example separator=, or separator=;.
quote Input The character placed around a field that contains the separator, normally quote=". Use quote=none if the file does not use quotes.
quote Output quote=none writes the values with no quotes. Any other value puts double quotes around every field.
endofline Output Leave out for lines ending in a line feed. endofline=crlf ends each line with carriage return and line feed, as Windows programs usually expect.

An example with all three, using ^ as the delimiter between options:

 <csvOptions>separator=;^quote=none^endofline=crlf</csvOptions>

CSV Output Configuration

      <map>
         <id>Map12</id>
         <enabled>true</enabled>
         <description>XML to CSV</description>
         <enableEmailNotifications>true</enableEmailNotifications>
         <connectors>
            <input>
               <id>in1</id>
               <description>Read XML File</description>
               <type>XML</type>
               <url>
                  <path>./interface/input/Map12 XML to CSV</path>
                  <mask/>
                  <prefix/>
                  <pollingInterval>1000</pollingInterval>
               </url>
               <xsl>
                  <XSLTPath/>
                  <XSLT>Map12_XML_to_CSV.xsl</XSLT>
               </xsl>
            </input>
            <output>
               <id>out1</id>
               <enabled>true</enabled>
               <description>Write CSV</description>
               <type>CSV</type>
               <url>
                  <path>./interface/output/Map12 XML to CSV</path>
                  <prefix>X</prefix>
                  <fileExtension>csv</fileExtension>
                  <use83GUID>true</use83GUID>
               </url>
               <csv>
                  <optionDelimeter>^</optionDelimeter>
                  <csvOptions>separator=,^quote=none</csvOptions>
               </csv>
            </output>
         </connectors>
      </map>
System Keys
Element Value Description
path Path Specifies where output files are saved.
type CSV Specify the output file format
optionDelimeter char See earlier description.
csvOptions var=val See earlier description.
fileExtension Blank or ext File extension to use for output file. Blank gives csv.
prefix String Option to force all output files to begin with a specific string prefix
use83GUID true or false For backwards compatibility with legacy apps which don't support long filenames. Generates a filename in 8.3 format
XSLT filename Optional style sheet for this output. Not used in this example, where the style sheet is on the input.

What the CSV Output Connector Expects

The style sheet must produce a <data> element containing <row> elements, and every row must have a cols attribute saying how many fields to write. This is the XML produced by Map12_XML_to_CSV.xsl in the example above:

 <data type="CSV">
    <row id="1" cols="13">
       <col id="1">P/20725187</col>
       <col id="2">43170672</col>
       <col id="3">Reverse Ratcheting Router</col>
       ...
    </row>
 </data>

Writing stops at the first row without a cols attribute, so if the attribute is missing the output file is empty. The XML made by the CSV input connector, shown further down this page, has no cols attribute on its rows. To write a CSV file from a CSV file, for example to change the separator or drop some columns, use a style sheet that copies the rows and adds the attribute.

CSV Example Input

GEN Comma Separated Variables 1.csv

01050213060215701000123780990210133980001,150213061500000017,15,15000,0210133,5021306021570,1008369,LS 2D  BLU,1000,00123780
01050213060215701000123780990210133980002,150213061500000017,15,15000,0210133,5021306021570,1008369,LS 2D  BLU,1000,00123780
01050213060215701000123780990210133980003,150213061500000017,15,15000,0210133,5021306021570,1008369,LS 2D  BLU,1000,00123780
01050213060215701000123780990210133980004,150213061500000017,15,15000,0210133,5021306021570,1008369,LS 2D  BLU,1000,00123780
01050213060215701000123780990210133980005,150213061500000017,15,15000,0210133,5021306021570,1008369,LS 2D  BLU,1000,00123780

CSV to XML Intermediate Format

 <?xml version="1.0" encoding="UTF-8"?>
 <data cols="10" filename="GEN Comma Separated Variables 1.csv" rows="5" type="CSV">
    <row id="1">
        <col id="1">01050213060215701000123780990210133980001</col>
        <col id="2">150213061500000017</col>
        <col id="3">15</col>
        <col id="4">15000</col>
        <col id="5">0210133</col>
        <col id="6">5021306021570</col>
        <col id="7">1008369</col>
        <col id="8">LS 2D BLU</col>
        <col id="9">1000</col>
        <col id="10">00123780</col>
    </row>
    <row id="2">
        <col id="1">01050213060215701000123780990210133980002</col>
        <col id="2">150213061500000017</col>
        <col id="3">15</col>
        <col id="4">15000</col>
        <col id="5">0210133</col>
        <col id="6">5021306021570</col>
        <col id="7">1008369</col>
        <col id="8">LS 2D BLU</col>
        <col id="9">1000</col>
        <col id="10">00123780</col>
    </row>
    <row id="3">
        <col id="1">01050213060215701000123780990210133980003</col>
        <col id="2">150213061500000017</col>
        <col id="3">15</col>
        <col id="4">15000</col>
        <col id="5">0210133</col>
        <col id="6">5021306021570</col>
        <col id="7">1008369</col>
        <col id="8">LS 2D BLU</col>
        <col id="9">1000</col>
        <col id="10">00123780</col>
    </row>
    <row id="4">
        <col id="1">01050213060215701000123780990210133980004</col>
        <col id="2">150213061500000017</col>
        <col id="3">15</col>
        <col id="4">15000</col>
        <col id="5">0210133</col>
        <col id="6">5021306021570</col>
        <col id="7">1008369</col>
        <col id="8">LS 2D BLU</col>
        <col id="9">1000</col>
        <col id="10">00123780</col>
    </row>
    <row id="5">
        <col id="1">01050213060215701000123780990210133980005</col>
        <col id="2">150213061500000017</col>
        <col id="3">15</col>
        <col id="4">15000</col>
        <col id="5">0210133</col>
        <col id="6">5021306021570</col>
        <col id="7">1008369</col>
        <col id="8">LS 2D BLU</col>
        <col id="9">1000</col>
        <col id="10">00123780</col>
    </row>
 </data>

XML Example Output from XSLT

 <?xml version="1.0" encoding="UTF-8"?>
 <test>
   <barcode>01050213060215701000123780990210133980001</barcode>
   <barcode>01050213060215701000123780990210133980002</barcode>
   <barcode>01050213060215701000123780990210133980003</barcode>
   <barcode>01050213060215701000123780990210133980004</barcode>
   <barcode>01050213060215701000123780990210133980005</barcode>
 </test>

See also: Connectors, Maps, Creating a Map