var url = "MyXML.xml"; //URL指向xml文件路径
createXMLHttpRequest();
XMLHttpReq.onreadystatechange = processResponse;
XMLHttpReq.open("GET",url,true); //get方法发送
XMLHttpReq.send(null); //请求
xml文件格式:
以下为引用的内容: <?xml version="1.0" encoding="GB2312"?> <classmates> <student> <sid>1</sid> <sname>reqe</sname> <gre>1700</gre> <tse>120</tse> </student> <student> <sid>2</sid> <sname>rqerwe</sname> <gre>1800</gre> <tse>120</tse> </student> <student> <sid>3</sid> <sname>iuir</sname> <gre>1700</gre> <tse>120</tse> </student> </classmates> |
以下为引用的内容: var students = XMLHttpReq.responseXML.getElementsByTagName("student"); //得到所有的student结点数组 for(var i =0;i<students.length;i++) { var stud = students[i]; //得一个stutdent结点 var name = stud.getElementsByTagName("sname")[0].firstChild.data; //取结点里的数据 var gre = stud.getElementsByTagName("gre")[0].firstChild.data; var tse = stud.getElementsByTagName("tse")[0].firstChild.data; } |