2013-12-10 192 views
0

我正在研究JSP中的項目。現在,我的程序成功地從XML文件中提取內容並將它們輸出到div中。我也有一些閱讀用戶輸入的文本框。我試圖將我的XML文件的內容更新爲用戶輸入到文本框中的內容,然後調用Names()方法將更新文件的內容輸出到div(替換舊內容)。但是,我的XML文件沒有更新,我不確定如何去做這件事。這裏是我的代碼:將用戶輸入寫入XML文件

<script src ="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<div id ="nameDiv"></div> 
<div id = "nameInput"> 

    <input type = "text" name = "nameOne" value = "Enter a Name" onClick = "if(this.value == 'Enter a Name'){this.value = '';}" /> 
<input type = "text" name="nameTwo" value = "Enter a Name" onClick = "if(this.value == 'Enter a Name'){this.value = '';}" /> 
    <input type ="text" name = "nameThree" value = "Enter a Name" onClick = "if(this.value == 'Enter a Name'){this.value = '';}" /> 
    <input type = "button" name = "btnUpdate" value = "Update Name" /></div> 

<% String name1 = request.getParameter("nameOne"); 
String name2 = request.getParameter("nameTwo"); 
String name3 = request.getParameter("nameThree"); 
File file = new File("Employees.xml"); 

public void updateXML(){ 

     if(name1 ! = '' || name2 != '' || name3 != ''){ 

     try{ 
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
      Document document = documentBuilder.parse(file); 

      Node firstNameLine = document.getElementsByTagName("People").item(0); 
      Node secNameLine = document.getElementsByTagName("People").item(1); 
      Node thirdNameLine = document.getElementsByTagName("People").item(2); 

      firstNameLine.setText(name1); 
      secNameLine.setText(name2); 
      thirdNameLine.setText(name3); 

      TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
      Transformer transformer = transformerFactory.newTransformer(); 
      transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
      StringWriter sw = new StringWriter(); 
      StreamResult result = new StreamResult(sw); 
      DOMSource domSource = new DOMSource(document); 

      transformer.transform(domSource, result); 
      String xmlString = sw.toString(); 
      FileWriter fw=new FileWriter(file,true); 
      BufferedWriter bw = new BufferedWriter(fw); 
      bw.write(xmlString); 
      bw.flush(); 
      bw.close(); 
      } 
      catch(Exception e) 
      { 

     } 
     } 
    } 
     public void buttonOnClick() throws ServletException, IOException { 

      if (request.getParameter("btnUpdate") != null) { 
       updateXML(); 
      } 
     } 
    %> 

function getOutput() 
{ 
    if (xmlhttp.readyState==4) 
    { 
    document.getElementById("nameInput").innerHTML=xmlhttp.responseText; 
    } 
} 

function loadXMLDoc(dname) { 


       if (window.XMLHttpRequest) { 
        xmlhttp = new XMLHttpRequest(); 
       } 

       else { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 

       xmlDoc.async = false; 
       xmlDoc.load(dname); 
       xmlDoc.save("Employees.xml"); 
       return (xmlDoc); 
     } 

function Names() { 
     var xml = getXML(); 
     var txt = ""; 
     $(xml).find("Person").each(function() { 
     txt += "<div>" + $(this).text() + "</div>"; 
     }); 
     $("body").append(txt); 
    } 

    Names(); 

我的XML文件:

<People> 
    <Person>George</Person> 
<Person>Mary</Person> 
<Person>John</Person> 
</People> 

現在,每次我打的更新按鈕,我只是得到了舊文件的內容被「粘貼」到DIV過並重新開始。它不會發布用戶輸入。我哪裏錯了?任何幫助,將不勝感激。

+0

這是代碼的全文嗎?你是不是錯過了'<%@page %>'等指令? – mschonaker

+0

這不是我所有的代碼(我省略了指令,導入等等),因爲我以爲我的錯誤與用戶輸入被回送有關。但我看不出它爲什麼不起作用的原因(我包括一個ajax調用)。我一直堅持這一點。 – Rebecca

+0

謝謝大家的所有幫助。 – Rebecca

回答

1

您確定表單實際上正在提交嗎? <input type = "button"將不會自行提交表單。嘗試改變type屬性submit

<input type = "submit" name = "btnUpdate" value = "Update Name" /></div> 
+0

感謝您的幫助! – Rebecca

1

我已經試過了這個例子,更新的人的數據與form.Please輸入的數據,看看這是否是幫助FUL你。

<%@page import="org.w3c.dom.Node"%> 
<%@page import="org.w3c.dom.NodeList"%> 
<%@page import="java.io.InputStreamReader"%> 
<%@page import="java.io.FileOutputStream"%> 
<%@page import="java.io.BufferedReader"%> 
<%@page import="java.io.FileReader"%> 
<%@page import="org.w3c.dom.Attr"%> 
<%@page import="javax.xml.transform.TransformerFactory"%> 
<%@page import="javax.xml.transform.Transformer"%> 
<%@page import="javax.xml.transform.dom.DOMSource"%> 
<%@page import="javax.xml.transform.stream.StreamResult"%> 
<%@page import="java.io.File"%> 
<%@page import="org.w3c.dom.Element"%> 
<%@page import="org.w3c.dom.Document"%> 
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%> 
<%@page import="javax.xml.parsers.DocumentBuilder"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<% 

    DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance(); 
    try { 

     String ufname = request.getParameter("fname"); 
     String ulname = request.getParameter("lname"); 

     DocumentBuilder docbuilder = dbfact.newDocumentBuilder(); 

     Document doc = docbuilder.newDocument(); 
     Element rootElement = doc.createElement("People"); 
     doc.appendChild(rootElement); 

     Element staff = doc.createElement("staff"); 
     rootElement.appendChild(staff); 

     Attr attr = doc.createAttribute("id"); 
     attr.setValue("1"); 

     Element firstname = doc.createElement("person"); 
     firstname.appendChild(doc.createTextNode(ufname)); 
     staff.appendChild(firstname); 

     /*Element firstname1 = doc.createElement("person"); 
     firstname1.appendChild(doc.createTextNode("Mahesh")); 
     staff.appendChild(firstname1);*/ 

     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     DOMSource source = new DOMSource(doc); 
     StreamResult result = new StreamResult(new File("D:\\file.xml")); 

     transformer.transform(source, result); 

     System.out.println("File saved!"); 

%> 

<% 

    File fXmlFile = new File("D:\\file.xml"); 

    Document docs = docbuilder.parse(fXmlFile); 
    docs.getDocumentElement().normalize(); 
    System.out.println("Root element :" + docs.getDocumentElement().getNodeName()); 

    NodeList nList = docs.getElementsByTagName("staff"); 
    for (int temp = 0; temp < nList.getLength(); temp++) { 
     Node nNode = nList.item(temp); 
     System.out.println("\nCurrent Element :" + nNode.getNodeName()); 
     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
      Element eElement = (Element) nNode; 
%> 
<%=eElement.getElementsByTagName("person").item(0).getTextContent()%> 
<% 
     } 
    } 
%> 
<% 

} catch (Exception e) { 
System.out.println("Exception" + e); 
} 
%>