2016-01-05 44 views
1

我想在運行時在我的web內容文件夾下創建一個XML文件,但是沒有顯示此類文件或目錄錯誤。如何在運行時創建xml文件?

我的代碼:

Document document = DocumentHelper.createDocument(); 
     Element rootElement = document.addElement("Students"); 
     Element studentElement = rootElement.addElement("student").addAttribute("country", "USA"); 
     studentElement.addElement("id").addText("1"); 
     studentElement.addElement("name").addText("Peter"); 

     XMLWriter writer = new XMLWriter(new FileWriter("/WebContent/Students.xml")); 
     //Note that You can format this XML document 
     /* 
     * FileWriter output = new FileWriter(new File("Students.xml")); 
     OutputFormat format = OutputFormat.createPrettyPrint(); 
     XMLWriter writer = new XMLWriter(output,format);<- will fomat the output 
     */ 

     //You can print this to the console and see what it looks like 
     String xmlElement = document.asXML(); 
     System.out.println(xmlElement); 
     writer.write(document); 
     writer.close(); 

我不知道如何做到這一點。任何人都可以幫助我解決我的代碼?

回答

0

我得到了答案,我只是將路徑從/WebContent/Students.xml更改爲 WebContent/Students.xml。

剛剛刪除/在WebContent之前