I have some data that I am getting from an excel file (CSV). For each row in the excel file I want to generate a different xml file.
The function I wrote takes in a string of headers an array list from the CSV file and a file name. I thinking that in my for loop that loops through the strings from CSV. I need to create a statement that can create multiple xml files and than I need to modify that chain in my main method. I need some guidance on how to create that statement
My main method and wrtietoXml method are pasted below Please offer some guidance
public class CsvToXml {
public static void main (String Args[]){
BufferedReader bufferedCsvFile = HelperMethods.getCsvFileBuffer("/Users/edgarjohnson/eclipse-workspace/CsvToXml/in.csv");
ArrayList<String> csvFileStrings = new ArrayList<String>();
HelperMethods.readCsvToStrings(csvFileStrings, bufferedCsvFile);
String[] columnHeaders = HelperMethods.setHeaders(csvFileStrings);
csvFileStrings.remove(0);
HelperMethods.writeXmlFile(columnHeaders, csvFileStrings, "xmlOutput.xml");
}
}
public static void writeXmlFile(String[] headers, ArrayList <String> stringsFromCsv, String fileName){
try {
BufferedWriter buffWrite = new BufferedWriter(new FileWriter(fileName));
buffWrite.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
buffWrite.write("<patient>\r\n");
for(String s:stringsFromCsv){
buffWrite.write("\t<person>\r\n");
String fields[] = s.split(",");
for(int i=0; i<fields.length; i++){
buffWrite.write("\t\t<" + headers[i] +">"+ fields[i] + "</" + headers[i] +">\n");
}
buffWrite.write("\t</person>\n");
}
buffWrite.write("</people>");
buffWrite.close();
}
catch (IOException ioe){ System.err.println("Error while writing to xml file in writeXmlFile: "); ioe.printStackTrace();
}
}
What I have tried:
I tried creating an array list and storing the xml tags in the array. Then iterating through the array and based on tag names separating in to multiple files. But I don't know how I would separate the code in to multiple files.