如何在运行时创建xml文件?

时间:2016-01-05 04:41:21

标签: java 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();

我不知道该怎么做。任何人都可以帮我修改我的代码吗?

1 个答案:

答案 0 :(得分:0)

我得到了答案我只是将路径从/WebContent/Students.xml更改为 的WebContent / Students.xml。

只需删除WebContent

之前的/