案例:用ajax 方法 解析xml
xml文件
<?xml version="1.0" encoding="utf-8"?>
<studentlist>
<student>
<name>tom</name>
<age>18</age>
<gender>男</gender>
</student>
<student>
<name>lucy</name>
<age>21</age>
<gender>女</gender>
</student>
<student>
<name>lilei</name>
<age>20</age>
<gender>男</gender>
</student>
<student>
<name>hanmeimei</name>
<age>18</age>
<gender>女</gender>
</student>
</studentlist>
----------------------------
05-getxml.html 使用ajax 解析xml 的文件
----
<body onload="getxml()">
<script>
function getxml(){
//创建异步对象
var xhr=new XMLHttpRequest();
//获取响应数据
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var result=xhr.responseXML;
console.log(result);
var studentlist=result.getElementsByTagName("studentlist")[0];
var student=studentlist.getElementsByTagName("student")[1];
var name=student.getElementsByTagName("name")[0];
console.log(name.innerHTML);
};
};
//打开连接创建请求
xhr.open("get","http://127.0.0.1:8080/students.xml",true);
//发送请求
xhr.send();
};
</script>
</body>