Bookmark Topic Watch Topic
  • New Topic

StringTokenizer to read CSV file

 
Ahamed Ali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,

I am trying to convert CSV to XML file, and I am using StringTokenizer to
read CSV file, but not clear understandble.

---------------------------CSV File-------------------------------------
[Header]
'devon','add','"LR","UOP"','test'
[Identification]
'northdevon.ac.uk','123456','',''
[Personal]
'Smith','West','Carol','Mrs','31/12/1977','F','156 Old Mill Lane, Worcester','WO34 5GG','ER234557H','','Favourite food?'
------------------------------------------------------------------

Can any one suggest me about this implementation or any links to have a similar examples.

I tried with StrinTokenizer, but could get the proper results.
-------------------------------------------------------------------------
public int transformMethod(String csvFileName, String xmlFileName){
int rowsCount=-1;
try{
Document newDoc = domBuilder.newDocument();
Element rootElement = newDoc.createElement("learnerinformation");
newDoc.appendChild(rootElement);

// Read comma seperated file
BufferedReader csvReader;
csvReader = new BufferedReader(new FileReader(csvFileName));
int fieldCount = 0;
String[] csvFields = null;
StringTokenizer stringTokenizer = null;

// Assumption: first line in CSV file is column/field names
// As the column names are used to name the elements in the
XML file,
// avoid using spaces/any other characters not suitable for
XML element naming
String curLine = csvReader.readLine();
if(curLine != null)
{
stringTokenizer = new StringTokenizer(curLine,",");
fieldCount = stringTokenizer.countTokens();
if(fieldCount > 0)
{
csvFields = new String[fieldCount];
int i=0;
while(stringTokenizer.hasMoreElements())
csvFields[i++] = String.valueOf( stringTokenizer.nextElement());
}
}

// Now we know the columns, Let's now read data row lines
while((curLine = csvReader.readLine()) != null)
{
stringTokenizer = new StringTokenizer(curLine,",");
fieldCount = stringTokenizer.countTokens();

if(fieldCount > 0)
{
Element rowElement = newDoc.createElement("Contenentype");
//Element rowElement1 = newDoc.createElement("referential");

int i=0;
while(stringTokenizer.hasMoreElements())
{
try
{
String curValue = String.valueOf(stringTokenizer.nextElement());
Element curElement = newDoc.createElement(csvFields[i++]);
curElement.appendChild(newDoc.createTextNode(curValue));
rowElement.appendChild(curElement);
// rowElement1.appendChild(curElement);
}
catch(Exception exp)
{
}
}
rootElement.appendChild(rowElement);
rowsCount++;
}
}
csvReader.close();
}
----------------------------------------------------------------------

thanks in advanced.


Ahamed
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same question in multiple forums. We want to see the discussion in one place, not waste time answering things that were already answered in another thread. Followups here. Thanks.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!