Reputation: 13
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);
}
}
Upvotes: 0
Views: 4049
Reputation: 2692
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.
Upvotes: 2