我正在研究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過並重新開始。它不會發布用戶輸入。我哪裏錯了?任何幫助,將不勝感激。
這是代碼的全文嗎?你是不是錯過了'<%@page %>'等指令? – mschonaker
這不是我所有的代碼(我省略了指令,導入等等),因爲我以爲我的錯誤與用戶輸入被回送有關。但我看不出它爲什麼不起作用的原因(我包括一個ajax調用)。我一直堅持這一點。 – Rebecca
謝謝大家的所有幫助。 – Rebecca