0
我正在使用下面的代碼在我的一個項目中使用Stax Parser創建XML文件。我之前在普通的java應用程序中使用過這段代碼,它在那裏運行的很好但是當我爲Servlet實現這個時,我不知道爲什麼它沒有運行。我沒有收到任何錯誤,但不生成XML文件。無法在Servlet中使用FileOutputStream創建新文件
private static ServletContext sc;
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
sc = config.getServletContext();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = sc.getRealPath("/WEB-INF/xml");
System.out.println("Path ==>" + path);
XMLOutputFactory factory = XMLOutputFactory.newInstance();
File f = new File(path+"/atms.xml");
XMLStreamWriter writer = factory.createXMLStreamWriter(new FileOutputStream(f));
//Some more code
writer.writeStartDocument();
writer.writeStartElement("xxx");
writer.writeStartElement("yyy");
writer.writeStartElement("id");
writer.writeCharacters("1");
writer.writeStartElement("name");
writer.writeCharacters("Table");
writer.writeStartElement("price");
writer.writeCharacters("110");
writer.writeEndElement();
writer.close();
}
調試servlet的。我看到代碼中沒有錯誤,除了'FileNotFoundException'的機會運行時錯誤 –