Ajax,读取服务器XML格式数据

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'showEmp.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">
	<script type="text/javascript">
		var xmlHttp;
		var x,xx;
		function showEmps(){
			if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}else{
				alert("暂不支持XMLHttpRequest对象");
				return;
			}
			if(!xmlHttp){
				alert("创建XMLHttpRequest对象失败!");
				return;
			}
			xmlHttp.onreadystatechange = callback;
			var url = '/ajax/servlet/EmpQueryServlet';
			xmlHttp.open("get",url,true);
			xmlHttp.send();
		}
		function callback(){
			if(xmlHttp.readyState == 4){
				if(xmlHttp.status == 200){
					var inHtml = "<table border='1px' style='border-collapse: collapse;' width='80%'>";
					inHtml += "<tr>";
						inHtml += "<th>部门</th>"
						inHtml += "<th>编号</th>"
						inHtml += "<th>姓名</th>"
						inHtml += "<th>工作</th>"
						inHtml += "<th>上级</th>"
						inHtml += "<th>就职日期</th>"
						inHtml += "<th>工资</th>"
					inHtml += "</tr>";
					x = xmlHttp.responseXML.documentElement.getElementsByTagName("person");						//xml文档对象
					for(var i=0; i < x.length; i++){
						inHtml += "<tr>";
							xx = x[i].getElementsByTagName("dname");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("empno");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("ename");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("job");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("mgr");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("hiredate");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
							
							xx = x[i].getElementsByTagName("sal");
							inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>";
						inHtml += "<tr>";
					}
					inHtml += "</table>";
					document.getElementById("showEmp").innerHTML = inHtml;
				}
			}
		}
	</script>
  </head>
  
  <body>
    <input type="button" onclick="showEmps()" value="显示所有员工" />
    <hr/>
    	<div id="showEmp"></div>
    <hr/>
    <table border="1px" style="border-collapse: collapse;" width="80%">
    </table>
  </body>
</html>

服务器XML文件

StringBuilder builder = new StringBuilder();
		builder.append("<emp>");
		for(int i = 0; i< empList.size(); i++){
			Object [] objects = (Object[])empList.get(i);
			builder.append("<person>");
				
				builder.append("<dname>"+objects[0].toString()+"</dname>");
				builder.append("<empno>"+objects[1].toString()+"</empno>");
				builder.append("<ename>"+objects[2].toString()+"</ename>");
				builder.append("<job>"+objects[3].toString()+"</job>");
				builder.append("<mgr>"+objects[4].toString()+"</mgr>");
				builder.append("<hiredate>"+objects[5].toString()+"</hiredate>");
				builder.append("<sal>"+objects[6].toString()+"</sal>");
			builder.append("</person>");
		}
		builder.append("</emp>");
		out.println(builder.toString());

相关文章

JS原生Ajax操作(XMLHttpRequest) GET请求 POST请求 兼容性问题 利用iframe模拟ajax 实现表单提交的返回...
AJAX 每日更新前端基础,如果觉得不错,点个star吧 &#128515; https://github.com/WindrunnerMax/E...
踩坑Axios提交form表单几种格式 前后端分离的开发前后端, 前端使用的vue,后端的安全模块使用的SpringSe...
很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈。 为了防止...
需要在服务器上进行哈 jquery的ajax方法: // jquery请求 $.ajax({ url: &quot;./server/slider.js...
Ajax函数封装ajax.js // Get / Post // 参数 get post // 是否异步 // 如何处理响应数据 // URL // var...