Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我在XML生成器中得到了重复的部分?_Xml_Jsp - Fatal编程技术网

为什么我在XML生成器中得到了重复的部分?

为什么我在XML生成器中得到了重复的部分?,xml,jsp,Xml,Jsp,我正在尝试使用JSP将表单数据存储在XML文件中。但是,在输入多个条目后,我得到了重复的行,如下所示: 似乎当记录2被追加时,记录2之前的全部内容都被重写了 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Students> <Name>test1</Name> <Title>balala</Title> <URL>www.kaka.com</

我正在尝试使用JSP将表单数据存储在XML文件中。但是,在输入多个条目后,我得到了重复的行,如下所示:

似乎当记录2被追加时,记录2之前的全部内容都被重写了

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Students>
<Name>test1</Name>
<Title>balala</Title>
<URL>www.kaka.com</URL>
</Students>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Students>
<Name>test1</Name>
<Title>balala</Title>
<URL>www.kaka.com</URL>
<Name>test2</Name>
<Title>nana</Title>
<URL>www.lolo.com</URL>
</Students>
我正在使用的代码:

<body>
    <h3>Get Data from the form</h3>
    <p>
    <%
       //Get data from the form
       String name= request.getParameter("name");
       String title=request.getParameter("title");
       String url= request.getParameter("url");
   %>


  <%!


      public void createXmlTree(String name,String title,String url) throws Exception {


        Element root;
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        File file = new File("C:/Users/webservstu/Documents/new.xml");
        if (file.exists())
        {
          doc = docBuilder.parse(file);
          root = doc.getDocumentElement();
          String sr = root.getNodeName();
        }
         else
        {

          System.out.println(name);
          root = doc.createElement("Students");
          doc.appendChild(root);
        }


        Element child1 = doc.createElement("Name");
        root.appendChild(child1);

        Text text1 = doc.createTextNode(name);
        child1.appendChild(text1);

        Element child2 = doc.createElement("Title");
        root.appendChild(child2);

        Text text2 = doc.createTextNode(title);
        child2.appendChild(text2);

        Element child3 = doc.createElement("URL");
        root.appendChild(child3);

        Text text3 = doc.createTextNode(url);
        child3.appendChild(text3);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");


        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(xmlString);
        bw.flush();
        bw.close();

    }%>


<%


  try
  {

    createXmlTree(name,title,url);

    out.println("<b>Xml File Created Successfully</b>");
  }
  catch(Exception e)
  {
    System.out.println(e);
  }

  %>

    <br>


    </p>
    <hr>

    <p><font size="2">*Direct Submission*</font></p>

    </body>
有什么想法吗?谢谢

这就是问题所在:

FileWriter fw=new FileWriter(file,true);
第二个参数是附加到文件末尾

另一方面,我建议不要仅仅更改此项—我建议改用FileOutputStream并直接从中创建StreamResult,这样它就可以直接将其写入文件,而不需要任何其他编码。实际上,您将使用平台默认编码来编写文本,即使文本本身声明它是UTF-8格式的


见鬼,您甚至不需要自己创建FileOutputStream—您可以直接从该文件创建StreamResult。

谢谢Jon,在我第一次尝试将第二个参数更改为false之后。成功了。但是,新添加的记录也包含在根类别Student中。用一个新的学生包装纸包装?对不起,我是个新手。谢谢。@EastBounder:正如我所说,无论如何,你应该改为不使用FileWriter。。。但是如果您想创建一个新的Student元素,您必须调用doc.createElementStudent并将所有元素添加到该元素中,而不是直接添加到根节点。