我尝试在运行时在我的 Web 内容文件夹下创建 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();
我不知道该怎么做。谁能帮我修复我的代码?
最佳答案
我得到了答案,我只是将路径从/WebContent/Students.xml 更改为 WebContent/Students.xml。
只需删除 WebContent 之前的/
关于java - 如何在运行时创建xml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34604263/