標籤: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、Firefox、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>
覺得不錯,推薦一下哦!
js裡各瀏覽器解析XML,支援IE、Firefox、Chrome等