Click here to Skip to main content
14,487,386 members
Rate this:
Please Sign up or sign in to vote.
See more:
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[]){
    	
        //This is hard coded, in a future version I would consider stripping this from a command-line arg.
        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); // Remove headers from the csvStrings Arraylist
        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");

           //String array that is the same size is that of the string from csv from line 66-69

           //For each string in the csv file

           for(String s:stringsFromCsv){

               buffWrite.write("\t<person>\r\n");

               //Split the line into an array of strings
               String fields[] = s.split(",");

               //For each item in that array of strings
               for(int i=0; i<fields.length; i++){
                   //Define a String and keep on adding to that string using field element array, String should be outside for loopLol

                   //Write the corresponding header to the file, as well as the value from the array 'fields'
                   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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100