修改嵌套的for循环以基于xml标签创建多个xml文件

时间:2018-07-11 17:20:11

标签: java xml csv

我有一些从excel文件(CSV)获取的数据。我要为excel文件中的每一行生成一个不同的xml文件。

我编写的函数从CSV文件和文件名中获取标题字符串和数组列表。我认为在我的for循环中,循环遍历CSV文件中的字符串。我需要创建一个语句,该语句可以基于excel文件中的行数或基于xml标记来创建多个xml文件,然后需要在我的main方法中修改该链。我需要一些有关如何创建该语句的指导。以下是我的in.csv文件。 分割后的每个文件都应命名为描述符_pdx01.xml,描述符_pdx02.xml,描述符_pdx03.xml ......等。

这也是in.csv文件的内容。

TESTCLIENT_2017_09_30    pdx01   Carcinosarcoma      C34448   M     12
TESTCLIENT_2017_09_30    pdx02   Esophageal carcinoma C4025     
TESTCLIENT_2017_09_30    pdx03   Esophageal carcinoma   C4025       45
TESTCLIENT_2017_09_30    pdx04   Carcinosarcoma       C34448    F   
TESTCLIENT_2017_09_30    pdx05   Esophageal carcinoma    C4025  F   60
TESTCLIENT_2017_09_30    pdx06   Esophageal carcinoma    C4025  F   66
TESTCLIENT_2017_09_30    pdx07   Carcinosarcoma       C34448    M    70

My in.csv File

我的主要方法和wrtietoXml方法粘贴在下面,让我知道您是否有任何想法

   import java.io.BufferedReader;
import java.util.ArrayList;


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();
        }



    }

0 个答案:

没有答案