本人做了一个把用户注册信息保存到xml文件的功能。然后再把xml文件里的数据读出来放到list.jsp里。并增加删除操作,可以删除相应的记录,删除后,xml文件里确实删除了相应的记录,但当我再次把注册信息提交的时候,list.jsp里删除的数据又全出来了,一直解决不了,望大虾指教。(我用的servlet。没用struts等框架。)
这是显示的servlet
public class DisplayInformList extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
String xmlFile = this.getServletContext().getRealPath("/")
+ "\\registerData\\" + "content.xml";
File file = new File(xmlFile);
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
try {
Document doc;
try {
doc = saxReader.read(file);
Element rootEle = doc.getRootElement(); // 获取文档根节点
List childEleList = rootEle.elements();
List storeInfoList = new ArrayList();
for (int i = 0; i < childEleList.size(); i++) {
Map dataAttrMap = new HashMap();
Element data = (Element) childEleList.get(i);
List dataAttrList = data.attributes();
String id = ((Attribute) dataAttrList.get(0)).getValue();
String danwei = ((Attribute) dataAttrList.get(1)).getValue();
String lianxiren = ((Attribute) dataAttrList.get(2)).getValue();
String suozaidiqu = ((Attribute) dataAttrList.get(3)).getValue();
String hangye = ((Attribute) dataAttrList.get(4)).getValue();
String dianhua = ((Attribute) dataAttrList.get(5)).getValue();
String bumen = ((Attribute) dataAttrList.get(6)).getValue();
String zhiwu = ((Attribute) dataAttrList.get(7)).getValue();
String youxiang = ((Attribute) dataAttrList.get(8)).getValue();
String shiyongchanpin = ((Attribute) dataAttrList.get(9)).getValue();
String content1 = ((Attribute) dataAttrList.get(10)).getValue();
dataAttrMap.put("id", id);
dataAttrMap.put("danwei", danwei);
dataAttrMap.put("lianxiren", lianxiren);
dataAttrMap.put("suozaidiqu", suozaidiqu);
dataAttrMap.put("hangye", hangye);
dataAttrMap.put("dianhua", dianhua);
dataAttrMap.put("bumen", bumen);
dataAttrMap.put("zhiwu", zhiwu);
dataAttrMap.put("youxiang", youxiang);
dataAttrMap.put("shiyongchanpin", shiyongchanpin);
dataAttrMap.put("content1", content1);
storeInfoList.add(dataAttrMap);
}
System.out.println("storeInfoList===="+storeInfoList.size());
request.setAttribute("storeInfoList", storeInfoList);
} catch (DocumentException e) {
e.printStackTrace();
}
request.getRequestDispatcher("/list.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request,response);
}
}
这是显示的servlet
public class DisplayInformList extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
String xmlFile = this.getServletContext().getRealPath("/")
+ "\\registerData\\" + "content.xml";
File file = new File(xmlFile);
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
try {
Document doc;
try {
doc = saxReader.read(file);
Element rootEle = doc.getRootElement(); // 获取文档根节点
List childEleList = rootEle.elements();
List storeInfoList = new ArrayList();
for (int i = 0; i < childEleList.size(); i++) {
Map dataAttrMap = new HashMap();
Element data = (Element) childEleList.get(i);
List dataAttrList = data.attributes();
String id = ((Attribute) dataAttrList.get(0)).getValue();
String danwei = ((Attribute) dataAttrList.get(1)).getValue();
String lianxiren = ((Attribute) dataAttrList.get(2)).getValue();
String suozaidiqu = ((Attribute) dataAttrList.get(3)).getValue();
String hangye = ((Attribute) dataAttrList.get(4)).getValue();
String dianhua = ((Attribute) dataAttrList.get(5)).getValue();
String bumen = ((Attribute) dataAttrList.get(6)).getValue();
String zhiwu = ((Attribute) dataAttrList.get(7)).getValue();
String youxiang = ((Attribute) dataAttrList.get(8)).getValue();
String shiyongchanpin = ((Attribute) dataAttrList.get(9)).getValue();
String content1 = ((Attribute) dataAttrList.get(10)).getValue();
dataAttrMap.put("id", id);
dataAttrMap.put("danwei", danwei);
dataAttrMap.put("lianxiren", lianxiren);
dataAttrMap.put("suozaidiqu", suozaidiqu);
dataAttrMap.put("hangye", hangye);
dataAttrMap.put("dianhua", dianhua);
dataAttrMap.put("bumen", bumen);
dataAttrMap.put("zhiwu", zhiwu);
dataAttrMap.put("youxiang", youxiang);
dataAttrMap.put("shiyongchanpin", shiyongchanpin);
dataAttrMap.put("content1", content1);
storeInfoList.add(dataAttrMap);
}
System.out.println("storeInfoList===="+storeInfoList.size());
request.setAttribute("storeInfoList", storeInfoList);
} catch (DocumentException e) {
e.printStackTrace();
}
request.getRequestDispatcher("/list.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request,response);
}
}
String xmlFile = this.getServletContext().getRealPath("/")
+ "\\registerData\\" + "content.xml";
File file = new File(xmlFile);
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
try {
doc = saxReader.read(file);
content = doc.getRootElement();
List dataEleList = content.elements();
Element dataChildElement;
for (int i = 0; i < dataEleList.size(); i++) {
dataChildElement = (Element) dataEleList.get(i); // 获取id属性
if (dataChildElement.attribute("id").getValue().equals(id)) { dataChildElement.getParent().remove(dataChildElement);
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter out = new XMLWriter(new FileOutputStream(
xmlFile), format);
out.write(doc);
out.close(); } catch (IOException e) {
System.out.println(e.getMessage());
}
break;
} }
} catch (DocumentException e) {
e.printStackTrace();
}
try {
request.getRequestDispatcher("/displayInformList").forward(request,
response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class AddInfoToXml extends HttpServlet {
private static final long serialVersionUID = 1L;
// Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
// 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
Document doc = null;
List<Document> list1 = new ArrayList<Document>();
Element content = null;
Element data = null;
private Integer id = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response) {
} public void doPost(HttpServletRequest request, HttpServletResponse response) {
String danweiBeforeTrans = request.getParameter("danwei");
String lianxirenBeforeTrans = request.getParameter("lianxiren");
String suozaidiquBeforeTrans = request.getParameter("suozaidiqu");
String hangyeBeforeTrans = request.getParameter("hangye");
String bumenBeforeTrans = request.getParameter("bumen");
String zhiwuBeforeTrans = request.getParameter("zhiwu");
String shiyongchanpinBeforeTrans = request.getParameter("shiyongchanpin");
String content1BeforeTrans = request.getParameter("content1");
String dianhua = request.getParameter("dianhua");
String youxiang = request.getParameter("youxiang");
String content1 = null;
String shiyongchanpin = null;
String zhiwu = null;
String bumen = null;
String hangye = null;
String suozaidiqu = null;
String lianxiren = null;
String danwei = null;
try {
danwei = new String(danweiBeforeTrans.getBytes("ISO-8859-1"),"UTF-8");
content1 = new String(content1BeforeTrans.getBytes("ISO-8859-1"),"UTF-8");
shiyongchanpin = new String(shiyongchanpinBeforeTrans.getBytes("ISO-8859-1"), "UTF-8");
zhiwu = new String(zhiwuBeforeTrans.getBytes("ISO-8859-1"), "UTF-8");
bumen = new String(bumenBeforeTrans.getBytes("ISO-8859-1"), "UTF-8");
hangye = new String(hangyeBeforeTrans.getBytes("ISO-8859-1"),"UTF-8");
suozaidiqu = new String(suozaidiquBeforeTrans.getBytes("ISO-8859-1"), "UTF-8");
lianxiren = new String(lianxirenBeforeTrans.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//数据放入注册信息实体对象ResiterData
ResiterData rd = new ResiterData();
rd.setContent1(content1);
rd.setShiyongchanpin(shiyongchanpin);
rd.setZhiwu(zhiwu);
rd.setBumen(bumen);
rd.setHangye(hangye);
rd.setSuozaidiqu(suozaidiqu);
rd.setLianxiren(lianxiren);
rd.setDanwei(danwei);
rd.setDianhua(dianhua);
rd.setYouxiang(youxiang);
String xmlFile = this.getServletContext().getRealPath("/")
+ "\\registerData\\" + "content.xml";
File file = new File(xmlFile);
if (file.exists()) {
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
Document document;
try {
document = saxReader.read(file);
list1.add(document);
} catch (DocumentException e) {
e.printStackTrace();
}
this.insertDataIntoXml(rd,"content.xml",list1);
} else {
doc = DocumentHelper.createDocument();
content = doc.addElement("content");
list1.add(doc);
this.insertDataIntoXml(rd,"content.xml",list1);
}
try {
request.getRequestDispatcher("/displayInformList").forward(request,
response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
} /**
* 往content.xml里插入数据
* @param rd
* @param fileName
* @param ll
*/
public void insertDataIntoXml(ResiterData rd, String fileName, List ll) {
doc = (Document) ll.get(0);
if (content == null) {
content = doc.getRootElement();
}
Element data = content.addElement("data");
data.setAttributeValue("id", (++id).toString());
data.setAttributeValue("danwei", rd.getDanwei());
data.setAttributeValue("lianxiren", rd.getLianxiren());
data.setAttributeValue("suozaidiqu", rd.getSuozaidiqu());
data.setAttributeValue("hangye", rd.getHangye());
data.setAttributeValue("dianhua", rd.getDianhua());
data.setAttributeValue("bumen", rd.getBumen());
data.setAttributeValue("zhiwu", rd.getZhiwu());
data.setAttributeValue("youxiang", rd.getYouxiang());
data.setAttributeValue("shiyongchanpin", rd.getShiyongchanpin());
data.setAttributeValue("content1", rd.getContent1());
ll.clear();
ll.add(doc); String realPath = this.getServletContext().getRealPath("/");
try {
OutputStream osw = new FileOutputStream(realPath + "\\registerData\\"
+ fileName);
Writer writer = new OutputStreamWriter(osw, "UTF-8");
doc.write(writer);
writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}