Class CsvToXml
- All Implemented Interfaces:
AdaptrisComponent
,ComponentLifecycle
,ComponentLifecycleExtension
,ConfigComment
,MessageEventGenerator
,Service
,StateManagedComponent
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.
-
Field Summary
Fields inherited from class com.adaptris.csv.transform.CsvXmlTransformImpl
CSV_FIELD_NAME, CSV_RECORD_NAME, XML_ROOT_ELEMENT
Fields inherited from class com.adaptris.core.ServiceImp
log
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionSpecify if element names should be derived from the first record.Specify whether or not to have unique element names for each record in the CSV file.void
setElementNamesFromFirstRecord
(Boolean elementNamesFromFirstRecord) Specify if element names should be derived from the first record.void
setUniqueRecordNames
(Boolean uniqueRecordNames) Specify whether or not to have unique element names for each record in the CSV file.protected Document
transform
(AdaptrisMessage msg) Methods inherited from class com.adaptris.csv.transform.CsvToXmlServiceImpl
addNewElement, buildPreferences, createTextNode, doService, getPreferenceBuilder, getStripIllegalXmlChars, setPreferenceBuilder, setStripIllegalXmlChars, stripIllegalXmlChars, writeXmlDocument
Methods inherited from class com.adaptris.csv.transform.CsvXmlTransformImpl
closeService, evaluateEncoding, getIncludeLineNumberAttribute, getOutputMessageEncoding, includeLineNumberAttribute, initService, prepare, safeElementNames, safeElementNames, setIncludeLineNumberAttribute, setOutputMessageEncoding
Methods inherited from class com.adaptris.core.ServiceImp
changeState, close, continueOnFailure, createName, createQualifier, getComments, getContinueOnFail, getIsTrackingEndpoint, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setComments, setContinueOnFail, setIsTrackingEndpoint, setUniqueId, start, stop
-
Constructor Details
-
CsvToXml
public CsvToXml()
-
-
Method Details
-
transform
- Specified by:
transform
in classCsvToXmlServiceImpl
- Throws:
ServiceException
-
getElementNamesFromFirstRecord
Specify if element names should be derived from the first record. -
setElementNamesFromFirstRecord
Specify if element names should be derived from the first record. -
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 calledrecord
with an associated attribute that declares the line number. -
setUniqueRecordNames
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 calledrecord
with an associated attribute that declares the line number.
-