我要写这样一个程序,对xml读写(增删改查)的功能。要求是:不使用框架(jQuery,struts)
使用servlet,使用ajax异步请求来完成。对于解析xml那块我已完成了。但是问题出现在了js那块上面。
我一直都没能解决。所以想大家帮帮我。
问题是:
我之前使用的是jQuery写的js代码。现在要求去掉框架。而我没写过,所以遇到了这个问题。
代码:
$(function(){
$.get("main.do",function(data){ //main.do是servlet的请求地址
var ids = data.getElementsByTagName("id");
var names = data.getElementsByTagName("name");
var ages = data.getElementsByTagName("age");
for(var i=0;i<ids.length;i++){
var id = ids[i].firstChild.nodeValue;
var name = names[i].firstChild.nodeValue;
var age = ages[i].firstChild.nodeValue;
$("#t1").append("<tr id='r"+id+"' ><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+
"<input type='button' value='del' id='d"+id+"'/>"+
" <input type='button' value='modify' id='m"+id+"'/></td></tr>");
}
$("#d" + id).click(function () {//这里是根据给的id值来删除一条记录
$.getJSON("delete.do", {"id":id});//可是,有问题存在,我删除不了记录,
//比如,我有三条记录,居然可以删除第三条,前面两条删除不了
alert("id:"+id);
$("#r" + id).remove();
alert("success");
});
$("#m" + id).click(function () {//这里是修改,我要把数据带过去。可是结果也不行
$.get("redirect.do",{"id":id},function(data){
alert("modify");
var f = "<form action='modify.do' method='post'><table>" +
"<tr><td>id</td><td><input type='text' id='id' name='id'/></td></tr>" +
"<td>name</td><td><input type='text' id='name' name='name'/></td></tr>" +
"<td>age</td><td><input type='text' id='age' name='age'/></td></tr></table></tr>" +
"<input type='submit' value='modify'/></form>";
$("#p2").empty();
$("#p2").append(f);
$("#id").val(data.id);
$("#name").val(data.name);
$("#age").val(data.age);
},"xml");
});
},"xml");
});
而最主要的是 我要把这些代码改成js写的 。不能使用jQuery框架。Servlet.java 代码:
public class EmpServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
XmlUtilDom xmlUtil = new XmlUtilDom();
String uri = request.getRequestURI();
String action = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
String path = EmpServlet.class.getClassLoader().getResource("employee.xml").getPath();
if(action.equals("/main")){
List<Employee> emps = xmlUtil.readXml();
request.getRequestDispatcher("xml/employee.xml").forward(request, response);
}
if(action.equals("/add")){
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
Employee emp = new Employee();
emp.setAge(Integer.parseInt(age));
emp.setName(name);
emp.setId(Integer.parseInt(id));
xmlUtil.writeXml(emp);
response.sendRedirect("main.html");
out.flush();
out.close();
}
if(action.equals("/redirect")){
String id = request.getParameter("id");
Employee emp = xmlUtil.findById(Integer.parseInt(id));
request.getRequestDispatcher(path).forward(request, response);
}
if(action.equals("/modify")){
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
Employee emp = xmlUtil.findById(Integer.parseInt(id));
emp.setName(name);
emp.setAge(Integer.parseInt(age));
xmlUtil.modifyXml(emp);
response.sendRedirect("main.html");
}
if(action.equals("/delete")){
String id = request.getParameter("id");
xmlUtil.deleteById(Integer.parseInt(id));
response.sendRedirect("main.html");
}
}
解析xml的代码:
XmlUtilDoom.java
public class XmlUtilDom {
//读取xml
public List<Employee> readXml() {
List<Employee> emps = new ArrayList<Employee>();
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
// System.out.println("path:"+path);
// InputStream file = XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
try {
Document doc = reader.read(file1);
List lists = doc.selectNodes("/emps/emp");
for (Object object : lists) {
Element e = (Element) object;
Element id = (Element) e.selectSingleNode("id");
Element name = (Element) e.selectSingleNode("name");
Element age = (Element) e.selectSingleNode("age");
Employee emp = new Employee();
emp.setId(Integer.parseInt(id.getText()));
emp.setName(name.getText());
emp.setAge(Integer.parseInt(age.getText()));
emps.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
return emps;
} //增加xml
public void writeXml(Employee employee) {
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
try {
// InputStream file=XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
Document doc = reader.read(file1);
Element root = (Element) doc.selectSingleNode("emps");
Element emp = root.addElement("emp");
Element id = emp.addElement("id");
Element name = emp.addElement("name");
Element age = emp.addElement("age");
id.setText(employee.getId().toString());
name.setText(employee.getName());
age.setText(employee.getAge().toString());
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
// System.out.println("write_path:"+path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//修改xml
public void modifyXml(Employee employee){
SAXReader reader = new SAXReader();
try {
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file = new File(path);
// InputStream file=XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
Document doc = reader.read(file);
List emps = doc.selectNodes("/emps/emp");
for (Object object : emps) {
Element e = (Element)object;
Element id = (Element)e.selectSingleNode("id");
if(Integer.parseInt(id.getText())==employee.getId()){
// System.out.println("modify");
Element name = (Element)e.selectSingleNode("name");
name.setText(employee.getName());
Element age = (Element)e.selectSingleNode("age");
age.setText(employee.getAge().toString());
}
}
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Employee findById(Integer id ){
List<Employee> emps = readXml();
for (Employee employee : emps) {
if(employee.getId() == id){
return employee;
}
}
return null;
}
//删除
public void deleteById(Integer id){
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
try {
Document doc = reader.read(file1);
Element root = (Element)doc.selectSingleNode("emps");
List list = doc.selectNodes("/emps/emp");
for(int i=0;i<list.size();i++){
Element emp = (Element)list.get(i);
Element eid = (Element) emp.selectSingleNode("id");
if(Integer.parseInt(eid.getText())==id){
root.remove(emp);
}
}
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
使用servlet,使用ajax异步请求来完成。对于解析xml那块我已完成了。但是问题出现在了js那块上面。
我一直都没能解决。所以想大家帮帮我。
问题是:
我之前使用的是jQuery写的js代码。现在要求去掉框架。而我没写过,所以遇到了这个问题。
代码:
$(function(){
$.get("main.do",function(data){ //main.do是servlet的请求地址
var ids = data.getElementsByTagName("id");
var names = data.getElementsByTagName("name");
var ages = data.getElementsByTagName("age");
for(var i=0;i<ids.length;i++){
var id = ids[i].firstChild.nodeValue;
var name = names[i].firstChild.nodeValue;
var age = ages[i].firstChild.nodeValue;
$("#t1").append("<tr id='r"+id+"' ><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+
"<input type='button' value='del' id='d"+id+"'/>"+
" <input type='button' value='modify' id='m"+id+"'/></td></tr>");
}
$("#d" + id).click(function () {//这里是根据给的id值来删除一条记录
$.getJSON("delete.do", {"id":id});//可是,有问题存在,我删除不了记录,
//比如,我有三条记录,居然可以删除第三条,前面两条删除不了
alert("id:"+id);
$("#r" + id).remove();
alert("success");
});
$("#m" + id).click(function () {//这里是修改,我要把数据带过去。可是结果也不行
$.get("redirect.do",{"id":id},function(data){
alert("modify");
var f = "<form action='modify.do' method='post'><table>" +
"<tr><td>id</td><td><input type='text' id='id' name='id'/></td></tr>" +
"<td>name</td><td><input type='text' id='name' name='name'/></td></tr>" +
"<td>age</td><td><input type='text' id='age' name='age'/></td></tr></table></tr>" +
"<input type='submit' value='modify'/></form>";
$("#p2").empty();
$("#p2").append(f);
$("#id").val(data.id);
$("#name").val(data.name);
$("#age").val(data.age);
},"xml");
});
},"xml");
});
而最主要的是 我要把这些代码改成js写的 。不能使用jQuery框架。Servlet.java 代码:
public class EmpServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
XmlUtilDom xmlUtil = new XmlUtilDom();
String uri = request.getRequestURI();
String action = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
String path = EmpServlet.class.getClassLoader().getResource("employee.xml").getPath();
if(action.equals("/main")){
List<Employee> emps = xmlUtil.readXml();
request.getRequestDispatcher("xml/employee.xml").forward(request, response);
}
if(action.equals("/add")){
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
Employee emp = new Employee();
emp.setAge(Integer.parseInt(age));
emp.setName(name);
emp.setId(Integer.parseInt(id));
xmlUtil.writeXml(emp);
response.sendRedirect("main.html");
out.flush();
out.close();
}
if(action.equals("/redirect")){
String id = request.getParameter("id");
Employee emp = xmlUtil.findById(Integer.parseInt(id));
request.getRequestDispatcher(path).forward(request, response);
}
if(action.equals("/modify")){
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
Employee emp = xmlUtil.findById(Integer.parseInt(id));
emp.setName(name);
emp.setAge(Integer.parseInt(age));
xmlUtil.modifyXml(emp);
response.sendRedirect("main.html");
}
if(action.equals("/delete")){
String id = request.getParameter("id");
xmlUtil.deleteById(Integer.parseInt(id));
response.sendRedirect("main.html");
}
}
解析xml的代码:
XmlUtilDoom.java
public class XmlUtilDom {
//读取xml
public List<Employee> readXml() {
List<Employee> emps = new ArrayList<Employee>();
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
// System.out.println("path:"+path);
// InputStream file = XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
try {
Document doc = reader.read(file1);
List lists = doc.selectNodes("/emps/emp");
for (Object object : lists) {
Element e = (Element) object;
Element id = (Element) e.selectSingleNode("id");
Element name = (Element) e.selectSingleNode("name");
Element age = (Element) e.selectSingleNode("age");
Employee emp = new Employee();
emp.setId(Integer.parseInt(id.getText()));
emp.setName(name.getText());
emp.setAge(Integer.parseInt(age.getText()));
emps.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
return emps;
} //增加xml
public void writeXml(Employee employee) {
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
try {
// InputStream file=XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
Document doc = reader.read(file1);
Element root = (Element) doc.selectSingleNode("emps");
Element emp = root.addElement("emp");
Element id = emp.addElement("id");
Element name = emp.addElement("name");
Element age = emp.addElement("age");
id.setText(employee.getId().toString());
name.setText(employee.getName());
age.setText(employee.getAge().toString());
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
// System.out.println("write_path:"+path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//修改xml
public void modifyXml(Employee employee){
SAXReader reader = new SAXReader();
try {
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file = new File(path);
// InputStream file=XmlUtil.class.getClassLoader().getResourceAsStream("employee.xml");
Document doc = reader.read(file);
List emps = doc.selectNodes("/emps/emp");
for (Object object : emps) {
Element e = (Element)object;
Element id = (Element)e.selectSingleNode("id");
if(Integer.parseInt(id.getText())==employee.getId()){
// System.out.println("modify");
Element name = (Element)e.selectSingleNode("name");
name.setText(employee.getName());
Element age = (Element)e.selectSingleNode("age");
age.setText(employee.getAge().toString());
}
}
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Employee findById(Integer id ){
List<Employee> emps = readXml();
for (Employee employee : emps) {
if(employee.getId() == id){
return employee;
}
}
return null;
}
//删除
public void deleteById(Integer id){
SAXReader reader = new SAXReader();
String path = XmlUtilDom.class.getClassLoader().getResource("employee.xml").getPath();
File file1 = new File(path);
try {
Document doc = reader.read(file1);
Element root = (Element)doc.selectSingleNode("emps");
List list = doc.selectNodes("/emps/emp");
for(int i=0;i<list.size();i++){
Element emp = (Element)list.get(i);
Element eid = (Element) emp.selectSingleNode("id");
if(Integer.parseInt(eid.getText())==id){
root.remove(emp);
}
}
OutputFormat out = OutputFormat.createPrettyPrint();
FileOutputStream fos = new FileOutputStream(path);
XMLWriter xmlWriter = new XMLWriter(fos,out);
xmlWriter.write(doc);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
解决方案 »
- 如何把json_encode($araAry)转换的php二维数组,动态赋值给js下拉列表!
- js校验一下字符串必须以 _INPUT 或 _OUTPUT结尾
- 字符串转json格式
- 点击图像热点区域,响应js点击事件的疑惑
- JS的几张图片切换功能,在IE下可运行,在firefox下就不行了
- <a href="#"></a>在IE中页面跳动了<a href="javascript:#"></a>在FF中警告
- 请教大家一个正则的问题,谢谢
- 关于js正则表达式问题
- 怎样学习从C/S转变为 B/S 的开发!!我是一个初学者,希望高手指明一条道!!
- 我用netscape 和 mozilla 和ie .不同的三种浏览器。怎样才能让打开的窗体对父窗体返回数据。
- 将页面导入到excel如何控制列宽
- 有没有js代码让文字有发光效果?
不知你发的代码有没有搞混。最好确认一下。
就是不能使用jQuery 框架
$(function)..
$.get()..
这不是在使用了jQuery框架吗?
我是想把他改成javascript的语法
是要用页面的请求 来对xml操作
这个要改成原生JS还不容易吗,应该知道吧。window.onload=function(){....};就好了。 $.get()..
找个原生的AJAX写法代替一下就是了。如果没搞混的话,你原来写的JQ代码掺了很多原生JS的写法,往好了说这可以提高执行效率,往坏了说,不知是否是对JQ的使用不熟,只为了方便书写AJAX就利用$.GET的这个部分。因为相比于加载JQ的代价来说,这些简单且不复杂或者量不多的原生JS所节省的效率并不突出。