Element el = new Element("comment"); Random rand = new Random(); int id = rand.nextInt(10000); el.setAttribute("id", "comment_" + id); el.setAttribute("attitude", attitude);
Element name = new Element("nikename"); CDATA cname = new CDATA(nikename); name.addContent(cname);
Element data = new Element("data"); CDATA ctext = new CDATA(comment); data.addContent(ctext);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); Text tdate = new Text(format.format(date)); Element pubdate = new Element("pubdate"); pubdate.addContent(tdate);
el.addContent(name); el.addContent(data); el.addContent(pubdate); root.addContent(el); XMLOutputter outputter = new XMLOutputter(" ", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try ...{ outputter.output(doc, new FileWriter(filepath)); } catch (IOException e) ...{ System.out.println("指定路径有错"); e.printStackTrace(); } return tdate.getText(); } /** *//** * 删除指定ID的评论 * @param commentId 评论ID * @return 返回操作结果字符串(成功或失败) * */ public String removeComment(String commentId) ...{ Element root = doc.getRootElement(); List comments = root.getChildren(); int size = comments.size(); Element dist = null; for (int i = 0; i size; i++) ...{ Element comment = (Element) comments.get(i); String id = comment.getAttributeValue("id"); if (id.equals(commentId)) ...{ dist = comment; break; } } if (dist != null) ...{ root.removeContent(dist); XMLOutputter outputter = new XMLOutputter(" ", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try ...{ outputter.output(doc, new FileWriter(filepath)); } catch (IOException e) ...{ System.out.println("重写文件有出错"); e.printStackTrace(); } return "成功删除指定元素!"; } else return "指定元素不存在!"; } /** *//** * 批量删除评论 * @param commentIdArgs 评论ID数组 * @return 返回操作结果字符串(成功或失败) * */ public String removeComments(String[] commentIdArgs) ...{ Element root = doc.getRootElement(); List comments = root.getChildren(); int size = comments.size(); int len = commentIdArgs.length; ListElement> dist = new ArrayListElement>(); outer:for (int i = 0; i size; i++) ...{ Element comment = (Element) comments.get(i); String id = comment.getAttributeValue("id");
for (int j = 0; j len; j++) if (id.equals(commentIdArgs[j])) ...{ dist.add(comment); continue outer; } } int dist_size=dist.size(); if (dist_size != 0) ...{ for (int i = 0; i dist_size; i++) root.removeContent(dist.get(i)); XMLOutputter outputter = new XMLOutputter(" ", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try ...{ outputter.output(doc, new FileWriter(filepath)); } catch (IOException e) ...{ System.out.println("重写文件有出错"); e.printStackTrace(); } return "成功删除指定的元素集合!"; } else return "指定元素集合的不存在!"; }
/** *//** * @return the filepath */ public String getFilepath() ...{ return filepath; }
/** *//** * @param filepath * the filepath to set */ public void setFilepath(String filepath) ...{ this.filepath = filepath; }
/** *//** * @return the builder */ public SAXBuilder getBuilder() ...{ return builder; }
/** *//** * @param builder * the builder to set */ public void setBuilder(SAXBuilder builder) ...{ this.builder = builder; } }
import com.ceun.bean.CommentBean; /** *//** * p>后台处理Servlet/p> *2007-01-30 * * @author ceun * 联系作者:br> * a href="mailto:ceun@163.com">ceun/a>br> * @version 1.0 2007-01-30 实现基本功能br> * */ public class AddCommentServlet extends HttpServlet ...{
/** *//** * serialVersionUID long */ private static final long serialVersionUID = 1L;
/** *//** * The doGet method of the servlet. br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter(); String nikename = request.getParameter("nn");
/** *//** * The doPost method of the servlet. br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{