I have an xml file that has different types of medical data on it. I am trying to split the xml file in to multiple files by tag name or by row. I am getting this xml data from a csv file. The xml file data looks like this.
CSV file that Im getting data from
Each file once split should be named descriptor_pdx01.xml, descriptor_pdx02.xml, descriptor_pdx03.xml ...... etc.
I am trying to accomplish this in Java using Buff Write or FileWriter. This is the code I wrote to write to the xml file. The problem is trying to figure out how to split the XML file in to multiple files in java using buffwrite and Filewriter
This is the content of the CSV FIle:
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();
}
}
If you could offer any guidance on how to do this it would be greatly appreciated.