JSP基于dom解析xml实例详解
程序员文章站
2023-12-24 17:08:45
本文实例讲述了jsp基于dom解析xml的方法。分享给大家供大家参考,具体如下:
初次学习用dom操作xml文件,有很多不足之处,牛人多给点建议,练习时我没对中文做乱码处...
本文实例讲述了jsp基于dom解析xml的方法。分享给大家供大家参考,具体如下:
初次学习用dom操作xml文件,有很多不足之处,牛人多给点建议,练习时我没对中文做乱码处理,也没做验证哦!o(∩_∩)o~
实体类:user
public class user { private string name; private string pwd; private string email; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpwd() { return pwd; } public void setpwd(string pwd) { this.pwd = pwd; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } }
数据访问层接口:userdao
public interface userdao { boolean login(string name, string pwd); void insertuser(user user); list<user> selectuser(); void updateuser(user user); boolean deleteuser(string name); public user findbyname(string name); }
接口实现类:userdaoimpl
public class userdaoimpl implements userdao { private static final string path="xml文件路径"; private void build(document dom) { try { //定义转换器 transformer f = transformerfactory.newinstance().newtransformer(); //设置输出的编码格式 f.setoutputproperty(outputkeys.encoding, "utf-8"); //构建dom源 domsource source = new domsource(dom); //指定文件存放的目标路劲 streamresult sr = new streamresult(new file(path)); //执行转换操作 f.transform(source, sr); } catch (exception e) { e.printstacktrace(); } } //登陆 public boolean login(string name, string pwd) { boolean flag = false; try { //根据以有的xml文件创建document对象 document dom = documentbuilderfactory.newinstance().newdocumentbuilder().parse(new file(path)); //获取user节点下的子节点 nodelist list = dom.getelementsbytagname("user"); //遍历list,数据匹配退出 for(int i = 0; i<list.getlength(); i++) { element el = (element)list.item(i); if(name.equals(el.getattribute("name")) && pwd.equals(el.getattribute("pwd"))) { flag = true; break; } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return flag; } //添加操作 public void insertuser(user user) { try { //创建全新的document对象 document dom = documentbuilderfactory.newinstance().newdocumentbuilder().newdocument(); //创建根节点users element el = dom.createelement("users"); //将根节点添加到dom中 dom.appendchild(el); //创建子节点 element e2 = dom.createelement("user"); //将子节点添加到根节点中 el.appendchild(e2); //取得xml文件中现有的信息 list<user> users = this.selectuser(); for(int i = 0; i < users.size(); i++){ //创建节点user element el3 = dom.createelement("user"); user us =users.get(i); //设置节点的属性(name,pwd,email) el3.setattribute("name", us.getname()); el3.setattribute("pwd", us.getpwd()); el3.setattribute("email", us.getemail()); //添加到根节点中 el.appendchild(el3); } e2.setattribute("name", user.getname()); e2.setattribute("pwd", user.getpwd()); e2.setattribute("email", user.getemail()); build(dom); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } } //查询操作 public list<user> selectuser() { list<user> userlist = new arraylist<user>(); try { //根据已有的xml文件创建dom document dom = documentbuilderfactory.newinstance().newdocumentbuilder().parse(new file(path)); //获取所有user节点下的子节点 nodelist list = dom.getelementsbytagname("user"); for(int i = 0;i <list.getlength();i++){ user user =new user(); element element = (element)list.item(i); user.setname(element.getattribute("name")); user.setpwd(element.getattribute("pwd")); user.setemail(element.getattribute("email")); userlist.add(user); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return userlist; } //修改操作 public void updateuser(user user) { try { //根据以有的xml文件创建dom document dom = documentbuilderfactory.newinstance().newdocumentbuilder().parse(new file(path)); //获取user节点下的子节点 nodelist list = dom.getelementsbytagname("user"); //遍历list for(int i = 0;i < list.getlength();i++) { element el = (element)list.item(i); //根据name属性去修改 if(user.getname().equals(el.getattribute("name"))) { el.setattribute("pwd", user.getpwd()); el.setattribute("email", user.getemail()); build(dom); } } } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } } //删除操作 public boolean deleteuser(string name) { try { //根据以有xml文件创建domcumet对象 document dom = documentbuilderfactory.newinstance().newdocumentbuilder().parse(new file(path)); //获取user节点下的子节点 nodelist list = dom.getelementsbytagname("user"); //遍历list for(int i=0;i<list.getlength();i++) { element el = (element)list.item(i); if(name.equals(el.getattribute("name"))) { el.getparentnode().removechild(el); build(dom); return true; } } } catch (exception e) { // todo: handle exception } return false; } //根据name查找 public user findbyname(string name) { user user = new user(); try { //根据以后的xml文件创建document对象 document dom = documentbuilderfactory.newinstance().newdocumentbuilder().parse(new file(path)); //获取user节点下的子节点集合 nodelist list = dom.getelementsbytagname("user"); for(int i=0;i<list.getlength();i++) { element el = (element)list.item(i); if(name.equals(el.getattribute("name"))) { user.setname(el.getattribute("name")); user.setpwd(el.getattribute("pwd")); user.setemail(el.getattribute("email")); break; } } } catch (exception e) { e.printstacktrace(); } return user; } }
业务逻辑层接口:userservice
boolean login(string name, string pwd); void insertuser(user user); list<user> selectuser(); void updateuser(user user); boolean deleteuser(string name); public user findbyname(string name);
接口实现类:userserviceimpl
public class userserviceimpl implements userservice { userdao dao = new userdaoimpl(); public boolean login(string name, string pwd) { return dao.login(name, pwd); } public void insertuser(user user) { dao.insertuser(user); } public list<user> selectuser() { return dao.selectuser(); } public void updateuser(user user) { dao.updateuser(user); } public boolean deleteuser(string name) { return dao.deleteuser(name); } public user findbyname(string name) { return dao.findbyname(name); } }
控制层:useraction
public class useraction extends actionsupport{ private user user; public user getuser() { return user; } public void setuser(user user) { this.user = user; } userservice userservice = new userserviceimpl(); public string selectuser(){ httpservletrequest request = servletactioncontext.getrequest(); list<user> users = new arraylist<user>(); users = userservice.selectuser(); request.setattribute("user", users); return "select"; } /** * 登陆 * @return */ public string login(){ if(user.getname() != null && user.getpwd() != null) { boolean flag = userservice.login(user.getname(), user.getpwd()); if(flag) { return success; } } return error; } /** * 修改 * @return */ public string update(){ userservice.updateuser(user); return "update"; } /** * 编辑 * @return */ public string edit(){ httpservletrequest request = servletactioncontext.getrequest(); string name = request.getparameter("uname"); if(name != null) { user u = userservice.findbyname(name); request.setattribute("user", u); } return "edit"; } /** * 删除 * @return */ public string delete(){ httpservletrequest request = servletactioncontext.getrequest(); string name = request.getparameter("uname"); boolean flag = userservice.deleteuser(name); system.out.println(flag); return success; } /** * 添加 * @return */ public string insert(){ userservice.insertuser(user); return "insert"; } }
struts.xml配置(本人用的的struts2):
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1.7//en" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="file" extends="struts-default"> <action name="list" class="com.jun.action.useraction" method="selectuser"> <result name="select">/list.jsp</result> </action> <action name="login" class="com.jun.action.useraction" method="login"> <result name="success" type="redirectaction">/list.action</result> <result name="error">/login.jsp</result> </action> <action name="insert" class="com.jun.action.useraction" method="insert"> <result name="insert" type="redirectaction">/list.action</result> </action> <action name="delete" class="com.jun.action.useraction" method="delete"> <result type="redirect">/list.action</result> </action> <action name="update" class="com.jun.action.useraction" method="update"> <result name="update" type="redirectaction">/list.action</result> </action> <action name="edit" class="com.jun.action.useraction" method="edit"> <result name="edit">/update.jsp</result> </action> </package> </struts>
web.xml配置
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
四个页面:login.jsp list.jsp insert.jsp,update.jsp
login.jsp
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="list.action">首页</a>|<a href="insert.jsp">注册</a> <form action="login.action" method="post"> <table> <tr> <td> 用户名:</td><td><input name="user.name" type="text"></td> </tr> <tr> <td> 密码:</td><td><input type="password" name="user.pwd"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登录"></td> </tr> </table> </form> </body> </html>
list.jsp
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body > <a href="insert.jsp">注册</a> <table border="1"> <tr> <td align="center" colspan="6"><font size="+3">用户列表</font></td> </tr> <tr> <td>序号</td><td>用户名</td><td>密码</td><td>邮箱</td><td>删除</td><td>编辑</td> </tr> <c:foreach items="${user}" var="u" varstatus="temp"> <tr> <td>${temp.index+1}</td><td>${u.name }</td><td>${u.pwd }</td><td>${u.email}</td><td><a href="delete.action?uname=${u.name}">删除</a></td><td><a href="edit.action?uname=${u.name }">编辑</a></td> </tr> </c:foreach> </table> </body> </html>
insert.jsp
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <% string path = request.getcontextpath(); string basepath = request.getscheme() + "://" + request.getservername() + ":" + request.getserverport() + path + "/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="list.action">首页</a> <form action="insert.action" method="post"> <table> <tr> <td> 用户名: </td> <td> <input name="user.name" type="text"> </td> </tr> <tr> <td> 密码: </td> <td> <input type="password" name="user.pwd"> </td> </tr> <tr> <td> 邮箱: </td> <td> <input type="text" name="user.email"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
update.jsp
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="list.action">首页</a> <h2>更新操作</h2> <form action="update.action" method="post"> <table> <c:set value="${user}" var="user"></c:set> <tr> <td> 用户名:</td><td><input readonly="readonly" name="user.name" value="${user.name}" type="text"></td> </tr> <tr> <td> 密码:</td><td><input type="password" value="${user.pwd }" name="user.pwd"></td> </tr> <tr> <td> 邮箱:</td><td><input type="text" value="${user.email }" name="user.email"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="保存"></td> </tr> </table> </form> </body> </html>
本人学习所用的user.xml文件
<?xml version="1.0" encoding="utf-8"?> <users> <user email=aaa@sina.com name="bbb" pwd="aaaaaa"/> <user email=bbb@sina.com name="ccc" pwd="aaaaaa"/> </users>
希望本文所述对大家jsp程序设计有所帮助。
推荐阅读