ajax的xml和json解析(原始)

来源:互联网 发布:windows 菜单栏 编辑:程序博客网 时间:2024/05/16 12:09


xml

package org.zy;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class StudentServlet extends HttpServlet{
Map<Integer,List<Student>>stus=null;
/**

*/
private static final long serialVersionUID = 8998843280728033433L;
public StudentServlet() {
stus=new HashMap<Integer,List<Student>>();
List<Student>us=new ArrayList<Student>();
us.add(new Student(1,"张益", 22));
us.add(new Student(2,"张益1", 221));
us.add(new Student(3,"张益3", 221));
stus.put(1, us);
us=new ArrayList<Student>();
us.add(new Student(11,"周杰伦", 22));
us.add(new Student(21,"王力宏", 221));
us.add(new Student(31,"陈奕迅", 221));
stus.put(2, us);
us=new ArrayList<Student>();
us.add(new Student(111,"詹姆斯", 22));
us.add(new Student(211,"韦德", 221));
us.add(new Student(311,"科比", 221));
stus.put(3, us);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//注意这里是xml
resp.setContentType("text/xml;charset=utf-8");
PrintWriter out=resp.getWriter();
int sid=Integer.parseInt(req.getParameter("sid"));
List<Student>ls=stus.get(sid);
StringBuffer sb=new StringBuffer();
sb.append("<students>");
for(Student stu:ls){
sb.append("<student>");
sb.append("<id>").append(stu.getId()).append("</id>");
sb.append("<username>").append(stu.getUsername()).append("</username>");
sb.append("<age>").append(stu.getAge()).append("</age>");
sb.append("</student>");
}
sb.append("</students>");
out.write(sb.toString());
}

}


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
        window.onload=function() {
            var stus = document.getElementById("stu");
            stus.onchange = getStudents;
        }
            function getStudents() {
                var value = this.value;
                var xhr = creatXMLHttpRequest();
                xhr.open("POST", "student.do", "true");
                xhr.onreadystatechange = function () {
                    var node = "";
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        var docxml = xhr.responseXML;
                        var stus = docxml.getElementsByTagName("student");
                        for (var i = 0; i < stus.length; i++) {
                            node += getProperties(stus[i], "id") + "-----"
                                    + getProperties(stus[i], "username") + "-----"
                                    + getProperties(stus[i], "age") + "-----<br/>"
                        }
                    }
                    document.getElementById("list01").innerHTML = node;
                }
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhr.send("sid=" + value);
            }
            function getProperties(obj, prop) {
                return obj.getElementsByTagName(prop)[0].childNodes[0].nodeValue;
            }
            function creatXMLHttpRequest() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
        
    </script>
</head>
<body>
<input id="stu" list="stus"/>
<datalist id="stus">
    <option value="1">帅哥队</option>
    <option value="2">明星队</option>
    <option value="3">nba队</option>
</datalist>
<div id="list01">


</div>
</body>
</html>

json

package org.zy;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class StudentServletJson extends HttpServlet{
Map<Integer,List<Student>>stus=null;
/**

*/
private static final long serialVersionUID = 8998843280728033433L;
public StudentServletJson() {
stus=new HashMap<Integer,List<Student>>();
List<Student>us=new ArrayList<Student>();
us.add(new Student(1,"张益1", 22));
us.add(new Student(2,"张益1", 221));
us.add(new Student(3,"张益3", 221));
stus.put(1, us);
us=new ArrayList<Student>();
us.add(new Student(11,"周杰伦1", 22));
us.add(new Student(21,"王力宏1", 221));
us.add(new Student(31,"陈奕迅1", 221));
stus.put(2, us);
us=new ArrayList<Student>();
us.add(new Student(111,"詹姆斯1", 22));
us.add(new Student(211,"韦德1", 221));
us.add(new Student(311,"科比1", 221));
stus.put(3, us);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//注意这里是xml
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
int sid=Integer.parseInt(req.getParameter("sid"));
List<Student>ls=stus.get(sid);
StringBuffer sb=new StringBuffer();
sb.append("[");
int index=0;
for(Student stu:ls){
if(index==0){
sb.append("{");
}else{
sb.append(",{");
}
sb.append("id:"+stu.getId()+",")
.append("username:\""+stu.getUsername()+"\",")
.append("age:"+stu.getAge()+"}");
index++;
}
sb.append("]");
out.write(sb.toString());
}
}


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
        window.onload=function() {
            var stus = document.getElementById("stu");
            stus.onchange = getStudents;
        }
            function getStudents() {
                var value = this.value;
                var xhr = creatXMLHttpRequest();
                xhr.open("POST", "studentJson.do", "true");
                xhr.onreadystatechange = function () {
                    var node = "";
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        var doc = xhr.responseText;
                        var str=eval(doc);
                        for (var i = 0; i < str.length; i++) {
                            node += str[i].id + "-----"
                                    + str[i].username + "-----"
                                    + str[i].age+ "-----<br/>"
                        }
                    }
                    document.getElementById("list01").innerHTML = node;
                }
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhr.send("sid=" + value);
            }
            function creatXMLHttpRequest() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
        
    </script>
</head>
<body>
<input id="stu" list="stus"/>
<datalist id="stus">
    <option value="1">帅哥队</option>
    <option value="2">明星队</option>
    <option value="3">nba队</option>
</datalist>
<div id="list01">


</div>
</body>
</html>

原创粉丝点击