student.xml文件如下
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student email="xiaobao@163.com">
<id>1</id>
<name>小宝</name>
</student>
<student email="xiaohong@163.com">
<id>2</id>
<name>小红</name>
</student>
</students>
jqueryxml.jsp文件如下
<!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" src="/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: "/xml/student.xml",
type: "GET",
dataType: "xml",
timeout: 1000, //设定超时时间
cache: false, //禁用缓存
success: function(xml) {
$(xml).find("student").each(function(i) { //查找所有student节点并遍历
var $name=$(this).children("name"); //获得name子节点
var name_value=$name.text(); //获取name节点文本
var $id = $(this).children("id"); //获得id子节点
var id_vaule = $id.text(); //获取id节点文本
var email_value = $(this).attr("email"); //获取节点的email属性
alert("id:"+id_vaule+" name:"+name_value+" email:"+email_value);
});
},
error: function() {
alert("加载XML文档出错!");
}
});
});
</script>
</head>
<body>
</body>
</html>