Class CsvToXml

  • All Implemented Interfaces:
    AdaptrisComponent, ComponentLifecycle, ComponentLifecycleExtension, MessageEventGenerator, Service, StateManagedComponent

    @ComponentProfile(summary="Transform a document from CSV to XML",
                      tag="service,transform,csv,xml",
                      since="3.11.0")
    public class CsvToXml
    extends CsvToXmlServiceImpl
    CSV to XML using net.sf.supercsv:super-csv.

    This transformation uses net.sf.supercsv:super-csv as the parsing engine for a CSV file to write each row as part of an XML document.

    If setElementNamesFromFirstRecord(Boolean) is true, then the first record is used to generate the element names; otherwise names are auto-generated based on the count of fields in the first row. Results are undefined if subsequent record contains more fields than the first record. If the first row contains a blank field then the blank is used as the element name. Note that if your header rows contains characters that would not be allowed in an standard XML element name then it will be replaced with an '_', so "Order Date" becomes "Order_Date".

    For example, given an input document :

     
     Event Name,Order Date,Ticket Type,Date Attending,Total Paid
     Glastonbury,"Sep 15, 2012",Free entry,"Jun 26, 2014 at 6:00 PM",0
     Reading Festival,"Sep 16, 2012",Free entry,"Aug 30, 2014 at 6:00 PM",0
     
     
    Then the output (without a header row, and with unique-record-names=true) would be
     
     <csv-xml>
       <record-1>
         <csv-field-1>Event Name</csv-field-1>
         <csv-field-2>Order Date</csv-field-2>
         <csv-field-3>Ticket Type</csv-field-3>
         <csv-field-4>Date Attending</csv-field-4>
         <csv-field-5>Total Paid</csv-field-5>
       </record-1>
       <record-2>
         <csv-field-1>Glastonbury</csv-field-1>
         <csv-field-2>Sep 15, 2012</csv-field-2>
         <csv-field-3>Free entry</csv-field-3>
         <csv-field-4>Jun 26, 2014 at 6:00 PM</csv-field-4>
         <csv-field-5>0</csv-field-5>
       </record-2>
       <record-3>
         <csv-field-1>Reading Festival</csv-field-1>
         <csv-field-2>Sep 16, 2012</csv-field-2>
         <csv-field-3>Free entry</csv-field-3>
         <csv-field-4>Aug 30, 2014 at 6:00 PM</csv-field-4>
         <csv-field-5>0</csv-field-5>
       </record-3>
     
     
    And with a header row specified (and unique-record-names = true):
     
     <csv-xml>
       <record-1>
         <Event_Name>Glastonbury</Event_Name>
         <Order_Date>Sep 15, 2012</Order_Date>
         <Ticket_Type>Free entry</Ticket_Type>
         <Date_Attending>Jun 26, 2014 at 6:00 PM</Date_Attending>
         <Total_Paid>0</Total_Paid>
       </record-1>
       <record-2>
         <Event_Name>Reading Festival</Event_Name>
         <Order_Date>Sep 16, 2012</Order_Date>
         <Ticket_Type>Free entry</Ticket_Type>
         <Date_Attending>Aug 30, 2014 at 6:00 PM</Date_Attending>
         <Total_Paid>0</Total_Paid>
       </record-2>
     
     

    In the adapter configuration file this class is aliased as csv-to-xml-transform which is the preferred alternative to the fully qualified classname when building your configuration.

    • Constructor Detail

      • CsvToXml

        public CsvToXml()
    • Method Detail

      • getElementNamesFromFirstRecord

        public java.lang.Boolean getElementNamesFromFirstRecord()
        Specify if element names should be derived from the first record.
      • setElementNamesFromFirstRecord

        public void setElementNamesFromFirstRecord​(java.lang.Boolean elementNamesFromFirstRecord)
        Specify if element names should be derived from the first record.
      • getUniqueRecordNames

        public java.lang.Boolean getUniqueRecordNames()
        Specify whether or not to have unique element names for each record in the CSV file.

        If there are multiple records in then each record can have a unique element name; e.g record-1, record-2, record-3 and so on. If false, then they are all called record with an associated attribute that declares the line number.

      • setUniqueRecordNames

        public void setUniqueRecordNames​(java.lang.Boolean uniqueRecordNames)
        Specify whether or not to have unique element names for each record in the CSV file.

        If there are multiple records in then each record can have a unique element name; e.g record-1, record-2, record-3 and so on. If false, then they are all called record with an associated attribute that declares the line number.