我正在研究JSP中的一个项目。现在,我的程序成功从XML文件中提取内容并将它们输出到div中。我也有一些读取用户输入的文本框。我试图将XML文件的内容更新为用户输入到文本框中的内容,然后调用Names()方法将更新文件的内容输出到div中(替换旧内容)。但是,我的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>
现在,每当我点击更新按钮,我只是得到旧文件的内容被粘贴&#34;一遍又一遍地进入div。它不会发布用户输入。我哪里错了?任何帮助将不胜感激。
答案 0 :(得分:1)
您确定该表单实际上已提交吗? <input type = "button"
不会自行提交表单。尝试将type
属性更改为submit
:
<input type = "submit" name = "btnUpdate" value = "Update Name" /></div>
答案 1 :(得分:1)
我已经尝试过这个例子,用表格中输入的数据来更新人的数据。如果对你有帮助,请查看这个。
<%@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);
}
%>