@ComponentProfile(summary="Easily transform a document from CSV to XML", tag="service,transform,csv,xml") public class SimpleCsvToXmlTransformService extends CsvToXmlServiceImpl
CSVParser
.
This transformation uses commons-csv as the parsing engine for a CSV
file. Basic parsing options are supported : DEFAULT
,
EXCEL
, RFC4180
,
MYSQL
and TAB DELIMITED
which correspond to
the base formats defined by CSVFormat
. Custom CSV formats are provided via CustomFormatBuilder
. In the event that
you have more complex requirements then you should be looking to use the FfTransformService
instead.
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 simple-csv-to-xml-transform which is the preferred alternative to the fully qualified classname when building your configuration.
CSV_FIELD_NAME, CSV_RECORD_NAME, XML_ROOT_ELEMENT
log
CONFIRMATION_ID_KEY
Constructor and Description |
---|
SimpleCsvToXmlTransformService() |
SimpleCsvToXmlTransformService(FormatBuilder f) |
Modifier and Type | Method and Description |
---|---|
java.lang.Boolean |
getElementNamesFromFirstRecord() |
java.lang.Boolean |
getUniqueRecordNames() |
void |
setElementNamesFromFirstRecord(java.lang.Boolean b)
Specify if element names should be derived from the first record.
|
void |
setUniqueRecordNames(java.lang.Boolean b)
Specify whether or not to have unique element names for each record in the CSV file.
|
protected org.w3c.dom.Document |
transform(AdaptrisMessage msg) |
addNewElement, createTextNode, doService, getFormat, getStripIllegalXmlChars, setFormat, setStripIllegalXmlChars, stripIllegalXmlChars, writeXmlDocument
closeService, evaluateEncoding, getIncludeLineNumberAttribute, getOutputMessageEncoding, includeLineNumberAttribute, initService, prepare, setIncludeLineNumberAttribute, setOutputMessageEncoding
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsConfirmation, getIsTrackingEndpoint, getLookupName, getUniqueId, init, isBranching, isConfirmation, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, rethrowServiceException, retrieveComponentState, setContinueOnFail, setIsConfirmation, setIsTrackingEndpoint, setLookupName, setUniqueId, start, stop
public SimpleCsvToXmlTransformService()
public SimpleCsvToXmlTransformService(FormatBuilder f)
protected org.w3c.dom.Document transform(AdaptrisMessage msg) throws ServiceException
transform
in class CsvToXmlServiceImpl
ServiceException
public java.lang.Boolean getElementNamesFromFirstRecord()
public void setElementNamesFromFirstRecord(java.lang.Boolean b)
b
- true to set the first record as the header.public java.lang.Boolean getUniqueRecordNames()
public void setUniqueRecordNames(java.lang.Boolean b)
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.
b
- true to generate a unique element name for each line in the CSV, default null (false)