1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Response</title> 6 </head> 7 <script> 8 /** 9 * 如果需要获得来自服务器的响应请使用XMLHttpRequest对象的responseText或responseXML属性 10 * 比如:document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 11 * 如果来自服务器的响应是XML并且需要作为XML对象进行解析,请使用responseXML属性 12 */ 13 function loadXMLDoc() 14 { 15 var xmlhttp; 16 var txt,x,i; 17 if (window.XMLHttpRequest) 18 {// code for IE7+, Firefox, Chrome, Opera, Safari 19 xmlhttp=new XMLHttpRequest(); 20 } 21 else 22 {// code for IE6, IE5 23 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 24 } 25 xmlhttp.onreadystatechange=function() 26 { 27 if (xmlhttp.readyState==4 && xmlhttp.status==200) 28 { 29 xmlDoc=xmlhttp.responseXML; 30 txt=""; 31 x=xmlDoc.getElementsByTagName("ARTIST"); 32 for (i=0;i<x.length;i++) 33 { 34 txt=txt + x[i].childNodes[0].nodeValue + "<br>"; 35 } 36 document.getElementById("myDiv").innerHTML=txt; 37 } 38 } 39 xmlhttp.open("GET","../cd_catelog.xml",true); 40 xmlhttp.send(); 41 } 42 </script> 43 <body> 44 <h2>My cd Collection:</h2> 45 <div id="myDiv"></div> 46 <button type="button" onclick="loadXMLDoc()">GET MY CD COLLECTION</button> 47 </body> 48 </html>
1 <CATALOG> 2 <CD> 3 <TITLE>Empire Burlesque</TITLE> 4 <ARTIST>Bob Dylan</ARTIST> 5 <COUNTRY>USA</COUNTRY> 6 <COMPANY>Columbia</COMPANY> 7 <PRICE>10.90</PRICE> 8 <YEAR>1985</YEAR> 9 </CD> 10 </CATALOG>