0

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.

Xml File Data

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.

  • See https://stackoverflow.com/questions/4142046/create-xml-file-using-java – PM 77-1 Jul 11 '18 at 19:26
  • While I do appreciate the suggestion I was wondering if it could be done using BuffWrite or FileWriter. Keep in mind the format of the excel file never changes. The excel file will always be in the above format. – Edgar Johnson Jul 11 '18 at 19:30
  • 2
    Don't write XML yourself until you *know* what you are doing, i.e. understand XML. Since you're not correctly encoding the text values in the XML, it seems you don't, so don't write the XML yourself. Heck, you're not even writing with the correct character set. Strongly recommend using one of Java's many API's for writing XML. – Andreas Jul 11 '18 at 19:39
  • "The excel file will always be in the above format" - How long have you known Microsoft? – Michael Kay Jul 12 '18 at 07:56
  • You can do this in XSLT 2.0 in half a dozen lines of code, and unlike your code, it will actually work. – Michael Kay Jul 12 '18 at 07:58
  • In reference to Brian I was talking about the fields in the CSV File. And really with only half a dozen lines?? – Edgar Johnson Jul 12 '18 at 13:46

1 Answers1

0

There are a lot of issues in your code as some people commented in your question.

I would use a lightweight library to handle both your CSV and XML files into a POJO.

My suggestion? BeanIO is a very good library to read/write CSV and XML files.

Write your POJO and use BeanIO's @Record and @Field annotations. BeanIO will gracefully parse both CSV / XML into your POJO's so you can do whatever you need.

randrade86
  • 346
  • 1
  • 10