0
我正嘗試將XML文件轉換爲帶有java的CSV格式,並將結果放入以當前日期和小時爲名稱的新目錄中。我是Java新手,直到現在我才成功創建目錄並進行轉換。任何人都可以告訴我如何正確地做到這一點,以便轉換後的文件將自動進入創建的目錄?感謝您的幫助。 這裏是我已經使用到現在的代碼:使用Java將XML轉換爲CSV信息
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);
}
}
將[使用java將XML文件轉換爲CSV文件]可能的重複(http://stackoverflow.com/questions/21413978/convert-an-xml-file-to-csv-file-using-java) – SachinSarawgi
i需要幫助我如何創建一個目錄,以便轉換後的文件自動進入 – LimeTech18
您需要幫助的部分。你能解釋更多嗎? – SachinSarawgi