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);
    }
}
5
  • 2
    Possible duplicate of Convert an XML file to CSV file using java Commented Dec 7, 2016 at 12:28
  • i need a help on how i could create a directory so that the converted file will go into it automatically Commented Dec 7, 2016 at 12:39
  • in which part of your you need help. Could you please explain more. Commented Dec 7, 2016 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?? Commented Dec 7, 2016 at 12:48
  • Thank you very much, it was exactly what i need. Best regard Commented Dec 7, 2016 at 13:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@LimeTech could I expect a up-vote, if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.