使用buffWrite或FileWriter将xml文件拆分为多个文件

时间:2018-07-11 19:22:22

标签: java xml

我有一个xml文件,上面有不同类型的医学数据。我正在尝试按标记名称或按行将xml文件拆分为多个文件。我从csv文件中获取此xml数据。 xml文件数据如下所示。

Xml File Data

CSV file that Im getting data from

每个分割后的文件应命名为descriptor_pdx01.xml,descriptor_pdx02.xml,descriptor_pdx03.xml ......等等。

我正在尝试使用Buff Write或FileWriter在Java中完成此操作。这是我编写的写入xml文件的代码。问题是试图找出如何使用buffwrite和Filewriter将XML文件拆分为java中的多个文件

这是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



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(",");


                String[] stringToWrite = fields;
                //Initiate a string variable and using the field array values form the xml and assign it to the string variable
                //DO a method call and Send the string and fileName (descriptor_field[1])

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



    }

如果您可以提供有关此操作的任何指导,将不胜感激。

1 个答案:

答案 0 :(得分:0)

有些人在您的问题中评论说,您的代码中有很多问题。

我将使用轻量级的库将CSV和XML文件都处理为POJO。

我的建议? BeanIO是一个很好的可读写CSV和XML文件的库。

编写您的POJO并使用BeanIO的std::string line; std::getline(cin, line) std::stringstream ss(line); while (ss >> val) { //... } <style> .subject { css: here } </style> echo '<div class="subject">' . $output['subject'] . '</div><br />'; 批注。 BeanIO会优雅地将CSV / XML解析为POJO,以便您可以执行所需的任何操作。