0

i am trying to convert XML file into a CSV format with java and put the result in a new directory with the current date and hour as name. I am new in Java and until now i have just succeed to create the directory and do the conversion. Can any one please tell me how i can do this correctly so that the converted file will automatically go into the created directory ? Thank for your help. Here is the code i have used until now:

    public static void main(String args[]) throws Exception {


        // Creating new directory in Java, if it doesn't exists

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");


        boolean success = false;
        // String time = dateFormat.format(date);
        String dir = "P:/export/";

        File directory = new File(dir + dateFormat.format(date));
        if (directory.exists()) {
            System.out.println("Directory already exists ...");

        }
        else {
            System.out.println("Directory not exists, creating now");

            success = directory.mkdir();
            directory.createNewFile();
            if (success) {
                System.out.printf("Successfully created new directory : %s%n", dir);
            }
            else {
                System.out.printf("Failed to create new directory: %s%n", dir);
            }
        }

        String AppDir = "P:/XML/";

        File stylesheet = new File(AppDir + "xsl/newTest.xsl");
        File xmlSource = new File(AppDir + "import/Tests/newTest.xml");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlSource);

        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = TransformerFactory.newInstance()
                                                    .newTransformer(stylesource);
        Source source = new DOMSource(document);
        Result outputTarget = new StreamResult(new File(AppDir + "export/newTest.csv"));
        transformer.transform(source, outputTarget);
    }
}
A Sdi
  • 665
  • 2
  • 10
  • 24
1467dtm
  • 13
  • 3
  • 7
  • 2
    Possible duplicate of [Convert an XML file to CSV file using java](http://stackoverflow.com/questions/21413978/convert-an-xml-file-to-csv-file-using-java) – SachinSarawgi Dec 07 '16 at 12:28
  • i need a help on how i could create a directory so that the converted file will go into it automatically – 1467dtm Dec 07 '16 at 12:39
  • in which part of your you need help. Could you please explain more. – SachinSarawgi Dec 07 '16 at 12:45
  • You could use like `String AppDir = directory.getAbsolutePath() + File.seperator+XML` in this way you will be directly inserting into your new created directory. Is this you want?? – SachinSarawgi Dec 07 '16 at 12:48
  • Thank you very much, it was exactly what i need. Best regard – 1467dtm Dec 07 '16 at 13:10

1 Answers1

2

Change your AppDir variable value to point to the new directory created as follows:

String AppDir = directory.getAbsolutePath() + File.seperator + XML + File.seperator;

In this way your all XML file will go inside newly created directory and then XML file directory.

SachinSarawgi
  • 2,632
  • 20
  • 28