我正在使用下面的代码在我的一个项目中使用 Stax Parser 创建 XML 文件。我之前在普通的 Java 应用程序中使用过这段代码,它在那里运行良好。但是当我为 Servlet 实现这个时,我不知道它为什么不运行。我没有收到任何错误,但没有生成XML文件。
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();
}