Hi, I was asked to create a program that converts a .csv (I converted it to a .txt format first) and I've put together a pretty basic one that reads the data in the text file and formats it into XML, then saves it as a .xml. By basic I mean it pretty much outputs...
<Person>
<Name>YouOver There</Name>
<Age>23</Age>
<Address>Sometimes Here</Address>
</Person>
I'm just wondering what I could add to my code that would make it more appealing and wouldn't require editing afterward? Such as adding namespaces, indent, etc. How could I add something like JDom into what I've got so far? And the examples I've seen from JDom show creating the XML document from scratch, how would I read a file into JDom similar to what I have at the minute? Here's the code I've managed so far...
package com.wh.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* This class will be used to read in a .txt file and then convert it to a .xml
* file. The .xml data will then be transfered over onto SoapUI to be used as
* mock test results. The class will have a method to read and write the files,
* as well as a method to do the conversion process. The process in this version
* would be to store the .txt in a scanner and run it in a while loop and output
* each line from an array that has the data stored. The headers will also be
* stored in a new array and printed to lower case.
*
* @version 1
* @author wh
*/
public class XMLGen
{
private File read, write;
private PrintWriter writer;
private Scanner scanner;
private String[] elements, headers;
private boolean firstRow = true;
private String rows;
/**
* This method will be used to read from the .txt file and then write to a
* .xml at specific directory paths. PrintWriter will be used to write the
* .xml.
*
* @throws FileNotFoundException
*/
public void readWrite() throws FileNotFoundException
{
// Reading in path file of .txt file.
read = new File("C:\\...");
// Storing inside a scanner.
scanner = new Scanner(read);
// Outputting to specific path file as a .xml file.
write = new File("C:\\...");
// Writes to the .xml file.
writer = new PrintWriter(write);
// Calling the method which carries out the converting processes.
doStuff();
// Outputs.
System.out.println("Reading from: " + read);
System.out.println("Writing to: " + write);
System.out.println("Done.");
}
/**
* This method will process the conversion by checking if the scanner (which
* stores the .txt data) has a next line. If there is a next line it will
* then take the rows and trim them, and then split each element from the
* separator in which case is the "," on the .txt file. The process then
* takes the first line and creates it as a header and moves on. This
* happens for each header. Then prints each element from the .txt into a
* simple .xml format and prints a blank line for readability purposes.
*/
public void doStuff()
{
// Reads in every line of the .txt file and converts to .xml format.
while (scanner.hasNext())
{
// Splitting the elements by removing the "," from the .txt file.
rows = scanner.nextLine().trim();
elements = rows.split(",");
// First line becomes the header then moves onto the next else continue
if (firstRow)
{
headers = elements.clone();
firstRow = false;
continue;
}
// Skips a line if there aren't enough elements.
if (elements.length < headers.length)
{
continue;
}
// Printing each element in XML format.
for (int count = 0; count < headers.length; ++count)
{
writer.println(" <" + headers[count].toLowerCase() + ">"
+ elements[count] + "</" + headers[count].toLowerCase()
+ ">");
}
// Blank space for readability.
writer.println();
}
// Closing writer.
writer.close();
// Closing scanner.
scanner.close();
}
}
Hopefully I explained what I'm trying to find out OK.
Thanks.