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 'index.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">
-->
<script language="javascript" src="<%=path%>/jslib/prototype.js"></script>
<script type="text/javascript">
//注意:在Servlet中要设置返回的是xml,而不是html;即 resp.setContentType("text/xml;charset=utf-8"); //resp.setContentType("text/html;charset=utf-8");
function sendXMLajax(){

var userName = "helloUserName";
var url = "<%=path%>/xmlServlet";
var params = "userName="+userName;
var myAjax = new Ajax.Request(url,
{method:'post',
parameters: params,
onComplete:showResultResponse,
asynchronous: false});
}

function showResultResponse(responseObj){
var resXML = responseObj.responseXML; //responseText
//alert(resXML);
var students = resXML.getElementsByTagName('student');
var studentsStr = "";
for (var i = 0; i < students.length; i++)
{
var student = students[i];
var idVal = student.getElementsByTagName("id")[0].firstChild.data;
var usernameVal = student.getElementsByTagName("username")[0].firstChild.data;
//alert("id="+idVal+"; username="+usernameVal);
studentsStr += "id="+idVal+"; username="+usernameVal+"<br/>";

}

$("resultDiv").innerHTML = studentsStr;


}
</script>
</head>

<body>
测试成功! 2009-04-28
<br/><br/>
<input type="button" name="btn" onclick="sendXMLajax()" value="xml测试"/> <br/><br/>

<div>
返回对象的结果: <div id="resultDiv"></div>
</div>
</body>
</html>

你可能感兴趣的:(JavaScript,xml,Ajax,jsp,css)