标签:des style blog http color io os ar strong
js在chrome中加载XML,js加载XML支持ff,IE6+,Opera等浏览器
见代码:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js里各浏览器解析XML,支持IE、火狐、Chrome等</title> </head> <body> <script> function loadxmlDoc(file){ try{ //IE xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); }catch(e){ ////Firefox, Mozilla, Opera, etc xmlDoc=document.implementation.createDocument("","",null); } try{ xmlDoc.async=false; xmlDoc.load(file);//chrome没有load方法 }catch(e){ //针对Chrome,不过只能通过http访问,通过file协议访问会报错 var xmlhttp = new window.XMLHttpRequest(); xmlhttp.open("GET",file,false); xmlhttp.send(null); xmlDoc = xmlhttp.responseXML.documentElement; } return xmlDoc; } var ob=loadxmlDoc("stu.xml"); var stu=ob.getElementsByTagName("stu"); for(var i=0;i<stu.length;i++){ var data=stu[i].childNodes; for(var j=0;j<data.length;j++){ if(data[j].nodeType==1){ if(document.all) document.write(data[j].text+" ");//IE else document.write(data[j].textContent+" "); } } document.write("<br/>"); } </script> </body> </html>
附stu.xml
<?xml version="1.0" encoding="UTF-8"?> <stulist> <stu> <name>张三</name> <age>20</age> <sex>男</sex> <classid>lamp87</classid> </stu> <stu> <name>李四</name> <age>22</age> <sex>女</sex> <classid>lamp86</classid> </stu> <stu> <name>王五</name> <age>21</age> <sex>女</sex> <classid>lamp87</classid> </stu> <stu> <name>王五</name> <age>21</age> <sex>女</sex> <classid>lamp87</classid> </stu> <stu> <name>赵六</name> <age>24</age> <sex>男</sex> <classid>lamp88</classid> </stu> </stulist>
觉得不错,推荐一下哦!
标签:des style blog http color io os ar strong
原文地址:http://www.cnblogs.com/52fhy/p/3973844.html