My csv file is a comma delimited file. I need to write XML contained in a variable to last cell of each row as part of my requirement. This XML contains commas(')
which when written through CSVWriter, writes incorrectly. each comma is treated as delimiter and the XML is split into different cells.
Part of my xml as sample
<tns:Nature>Residential</tns:Nature>
<tns:Type>Duplex</tns:Type>
<tns:OccupancyPurpose>Owner Occupied Primary Residence</tns:OccupancyPurpose>
<tns:Valuation>
<tns:Sequence>1</tns:Sequence>
<tns:ValuationAmount>700000</tns:ValuationAmount>
<tns:ValuationDate>2014-08-13</tns:ValuationDate>
<tns:ValuerCompany>C J A **Replace_Comma** Lee **Replace_Comma** Property</tns:ValuerCompany>
<tns:ValuationType>Electronic Assessment</tns:ValuationType>
</tns:Valuation>
My code goes as below.
strLine = strLine + strXMLContent;
File file = new File(strFilePath);
FileWriter outputfile = null;
try
{
outputfile = new FileWriter(file,true);
CSVWriter writer = new CSVWriter(outputfile,',', CSVWriter.NO_QUOTE_CHARACTER,CSVWriter.DEFAULT_ESCAPE_CHARACTER,System.getProperty("line.separator"));;
String arr[] = strLine.split(",",-1);
for(int i = 0; i< arr.length; i++)
{
arr[i] = arr[i].replace("Replace_Comma", ","); //Replaces filler as a comma before being placed in csv
}
writer.writeNext(arr);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
The filler when translated as a comma is treated as a delimiter and the following text is written in the next cell.
I have tried many things but to no avail. Please help.