<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> function f1() { var obj = new XMLHttpRequest(); obj.onreadystatechange = function() { if (obj.readyState == 4 && obj.status == 200) { var xmlobj = obj.responseXML; var students = xmlobj.childNodes[0]; var student = students.getElementsByTagName('student'); var info = ""; for (var i = 0; i < student.length; i++) { var name = student[i].getElementsByTagName('name')[0].firstChild.nodeValue; var addr = student[i].getElementsByTagName('addr')[0].firstChild.nodeValue; var age = student[i].getElementsByTagName('age')[0].firstChild.nodeValue; info += "姓名:" + name + ",地址:" + addr + ",年龄:" + age + "<br/>"; } document.getElementById('result').innerHTML = info; } } obj.open("get", "1.xml?"+Math.random()); obj.send(); } </script> </head> <body> <h2>Ajax获取xml信息</h2> <div id="result"></div> <input type="button" name="" onclick="f1()" value="触发"> </body> </html>
xml
<?xml version="1.0" encoding= "utf-8" ?> <students> <student> <name>Wendy</name> <age>35</age> <addr>Santa Fe</addr> </student> <student> <name>Yaphet</name> <age>32</age> <addr>Balchik</addr> </student> <student> <name>Isaiah</name> <age>6000</age> <addr>Caldera</addr> </student> </students>
李晓健
慕的地6079101
慕粉0957076526
李晓健