Ajax 解析 Xml

student.xml

<?xml version="1.0" encoding="UTF-8"?>
<classmates>
<student>
<sid>1</sid>
<sname>张家园</sname>
<gre>1700</gre>
<tse>120</tse>
</student>

<student>
<sid>1</sid>
<sname>贾宝玉</sname>
<gre>1800</gre>
<tse>120</tse>
</student>

<student>
<sid>1</sid>
<sname>林黛玉</sname>
<gre>1800</gre>
<tse>120</tse>
</student>
</classmates>


xml.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
var xmlHttpRequest;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){

}
}
}
}

//发送请求的函数
function sendRequest(){
alert('jjj');
var url = "student.xml";
alert("jjjjjjjjj");
createXMLHttpRequest();
xmlHttpRequest.onreadystatechange=processResponse;
xmlHttpRequest.open("GET",url,true);
xmlHttpRequest.send(null);
}

//处理响应的函数
function processResponse(){
if(xmlHttpRequest.readyState == 4){
if(xmlHttpRequest.status == 200){
readXml();
}else{
window.alert("你的请求内容有错!");
}
}
}

function readXml(){
var students = xmlHttpRequest.responseXML.getElementsByTagName("student");
for(var i = 0 ; i < students.length ; i++){
var stu = students[i];
var name = stu.getElementsByTagName("sname")[0].firstChild.data;
var gre = stu.getElementsByTagName("gre")[0].firstChild.data;
var tse = stu.getElementsByTagName("tse")[0].firstChild.data;
document.writeln(name);
document.writeln(gre);
document.writeln(tse);
}
}
</script>
</head>
<body>
<table>
<tr>
<td>
<input type="button" value="测试" οnclick="sendRequest();" />
</td>
</tr>
</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值