CSV2XML java converter without packages?
i found this online it looks correct but i cant seem to get it to work. If i comment out that whole try section ("System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>");") i receive an array out of bounds.
but with this code it jumps into this try and i receive "Usage: java CSV2XML <inputCSVFile> <outputXMLFile>" I think it may be how i have created the input and output folders as these are the only things ive really changed.
Code:
package newpackage;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class CSV2XML {
// Protected Properties
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
// CTOR
public CSV2XML()
{
try
{
domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
}
catch(FactoryConfigurationError exp)
{
System.err.println(exp.toString());
}
catch(ParserConfigurationException exp)
{
System.err.println(exp.toString());
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
}
public int convertFile(String csvFileName, String xmlFileName)
{
int rowsCount = -1;
try
{
Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("CSV2XML");
newDoc.appendChild(rootElement);
// Read comma seperated file
BufferedReader csvReader;
csvFileName = "C:\\ryan0j3\\test.csv";
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, now read data row lines
while((curLine = csvReader.readLine()) != null)
{
stringTokenizer = new StringTokenizer(curLine, ",");
fieldCount = stringTokenizer.countTokens();
if(fieldCount > 0)
{
Element rowElement = newDoc.createElement("row");
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);
}
catch(Exception exp)
{
}
}
rootElement.appendChild(rowElement);
rowsCount++;
}
}
csvReader.close();
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(newDoc);
xmlFileName = "C:\\ryan0j3\\test.xml";
Result dest = new StreamResult(new File(xmlFileName));
aTransformer.transform(src, dest);
rowsCount++;
}
catch(IOException exp)
{
System.err.println(exp.toString());
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
return rowsCount;
}
public static void main(String[] args)
{
try
{
if(args.length != 2)
{
System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>");
return;
}
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
try
{
CSV2XML csvConverter = new CSV2XML();
int rowsCount = csvConverter.convertFile(args[0], args[1]);
if(rowsCount >= 0)
{
System.out.println("CSV File " + args[0] +
"' successfully converted to XML File "+ args[1] + "\n" +
"(" + String.valueOf(rowsCount) + " rows)");
}
else
{
System.out.println("Error while converting input CSV File " + args[0] +
" to output XML File "+ args[1] + " ");
}
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
}
}
Re: CSV2XML java converter without packages?
month,year,value
january,1990,30000
july,1991,70000
january,1992,120000
july,1993,163333
january,1994,208333
july,1995,253333
january,1996,298333
july,1997,343333
january,1998,388333
july,1999,433333
this is my csv file.
Also this is the error code if i comment out the section that its going to.
java.lang.ArrayIndexOutOfBoundsException: 0
I assumed it was something to do with the way im trying to input/output the file as that was the only thing i seemed to change. Any help would be muchly appreciated or even a push int he right direction.
Re: CSV2XML java converter without packages?
How are you running this?
Are you running this the way that message says?
java CSV2XML <inputCSVFile> <outputXMLFile>
Re: CSV2XML java converter without packages?
If you want to hardcode the names of the files in the program, add something like this after line 135:
args = new String[] {"input filename here", "output filename here"}; // define files in program
Re: CSV2XML java converter without packages?
line 54 and line 115 is were i tried specifying the file.
I'm just running it in netbeans and also tried eclipse.
Re: CSV2XML java converter without packages?
Did it work when you coded it your way?
Re: CSV2XML java converter without packages?
nope, I'm not quite sure what you meant with your post either as its already used the input file and read and wrote the file by line 115. line 115 is just used to saved the file as a xml unless I completely have the wrong impression of this code.
Re: CSV2XML java converter without packages?
Code:
int rowsCount = csvConverter.convertFile(args[0], args[1]);
You want to either do as Norm suggests, which will create an args[] where args[0] is the input file and args[1] is the output file, or simply replace that call with this, defining the two files in the call:
Code:
int rowsCount = csvConverter.convertFile("<input file>", "<output file>");
Re: CSV2XML java converter without packages?
Thanks norm it worked great, so line 54 and 115 are not need ?
Re: CSV2XML java converter without packages?
My code gives the program some filenames to use and makes it easier to quickly change where the filenames are specified. The other changes bury the changes in the code which can make for harder maintenance.
Re: CSV2XML java converter without packages?
Yer i assumed it was something todo with were my input and output was i removed mine on the lines i said, It still works fine I sort of understand how it works I been trying to work it out for a couple of days now. Thanks alot.