首页 > 解决方案 > 修改嵌套for循环,根据xml标签创建多个xml文件

问题描述

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

我编写的函数从 CSV 文件和文件名中获取一串标题和一个数组列表。我认为在我的 for 循环中循环遍历 CSV 文件中的字符串。我需要创建一个语句,该语句可以根据 excel 文件中的行数或基于 xml 标签创建多个 xml 文件,然后我需要在我的 main 方法中修改该链。我需要一些关于如何创建该语句的指导。下面附上我的 in.csv 文件。每个分割后的文件应命名为descriptor_pdx01.xml、descriptor_pdx02.xml、descriptor_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

我的 in.csv 文件

我的main方法和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();
        }



    }

标签: javaxmlcsv

解决方案


推荐阅读