首页 > 开发 > AJAX > 正文

JS使用ajax从xml文件动态获取数据显示的方法

2024-09-01 08:32:59
字体:
来源:转载
供稿:网友

这篇文章主要介绍了JS使用ajax从xml文件动态获取数据显示的方法,实例分析了javascript使用Ajax技术操作XML文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JS使用ajax从xml文件动态获取数据显示的方法。分享给大家供大家参考。具体分析如下:

下面的JS代码通过ajax检索xml文件的内容动态展示到网页,真个页面无刷新

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <script> 
  5. function loadXMLDoc(url) 
  6. var xmlhttp; 
  7. var txt,x,xx,i; 
  8. if (window.XMLHttpRequest) 
  9. {// code for IE7+, Firefox, Chrome, Opera, Safari 
  10. xmlhttp=new XMLHttpRequest(); 
  11. else 
  12. {// code for IE6, IE5 
  13. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  14. xmlhttp.onreadystatechange=function() 
  15. if (xmlhttp.readyState==4 && xmlhttp.status==200) 
  16. txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>"
  17. x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD"); 
  18. for (i=0;i<x.length;i++) 
  19. txttxt=txt + "<tr>"; 
  20. xxx=x[i].getElementsByTagName("TITLE"); 
  21. try 
  22. txttxt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>"; 
  23. catch (er) 
  24. txttxt=txt + "<td> </td>"; 
  25. xxx=x[i].getElementsByTagName("ARTIST"); 
  26. try 
  27. txttxt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>"; 
  28. catch (er) 
  29. txttxt=txt + "<td> </td>"; 
  30. txttxt=txt + "</tr>"; 
  31. txttxt=txt + "</table>"; 
  32. document.getElementById('txtCDInfo').innerHTML=txt
  33. xmlhttp.open("GET",url,true); 
  34. xmlhttp.send(); 
  35. </script> 
  36. </head> 
  37. <body> 
  38. <div id="txtCDInfo"> 
  39. <button onclick="loadXMLDoc('cd_catalog.xml')">Get CD info</button> 
  40. </div> 
  41. </body> 
  42. </html> 

xml文件内容如下

 

 
  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. <CD> 
  11. <TITLE>Hide your heart</TITLE> 
  12. <ARTIST>Bonnie Tyler</ARTIST> 
  13. <COUNTRY>UK</COUNTRY> 
  14. <COMPANY>CBS Records</COMPANY> 
  15. <PRICE>9.90</PRICE> 
  16. <YEAR>1988</YEAR> 
  17. </CD> 
  18. <CD> 
  19. <TITLE>Greatest Hits</TITLE> 
  20. <ARTIST>Dolly Parton</ARTIST> 
  21. <COUNTRY>USA</COUNTRY> 
  22. <COMPANY>RCA</COMPANY> 
  23. <PRICE>9.90</PRICE> 
  24. <YEAR>1982</YEAR> 
  25. </CD> 
  26. <CD> 
  27. <TITLE>Still got the blues</TITLE> 
  28. <ARTIST>Gary Moore</ARTIST> 
  29. <COUNTRY>UK</COUNTRY> 
  30. <COMPANY>Virgin records</COMPANY> 
  31. <PRICE>10.20</PRICE> 
  32. <YEAR>1990</YEAR> 
  33. </CD> 
  34. <CD> 
  35. <TITLE>Eros</TITLE> 
  36. <ARTIST>Eros Ramazzotti</ARTIST> 
  37. <COUNTRY>EU</COUNTRY> 
  38. <COMPANY>BMG</COMPANY> 
  39. <PRICE>9.90</PRICE> 
  40. <YEAR>1997</YEAR> 
  41. </CD> 
  42. <CD> 
  43. <TITLE>One night only</TITLE> 
  44. <ARTIST>Bee Gees</ARTIST> 
  45. <COUNTRY>UK</COUNTRY> 
  46. <COMPANY>Polydor</COMPANY> 
  47. <PRICE>10.90</PRICE> 
  48. <YEAR>1998</YEAR> 
  49. </CD> 
  50. <CD> 
  51. <TITLE>Sylvias Mother</TITLE> 
  52. <ARTIST>Dr.Hook</ARTIST> 
  53. <COUNTRY>UK</COUNTRY> 
  54. <COMPANY>CBS</COMPANY> 
  55. <PRICE>8.10</PRICE> 
  56. <YEAR>1973</YEAR> 
  57. </CD> 
  58. <CD> 
  59. <TITLE>Maggie May</TITLE> 
  60. <ARTIST>Rod Stewart</ARTIST> 
  61. <COUNTRY>UK</COUNTRY> 
  62. <COMPANY>Pickwick</COMPANY> 
  63. <PRICE>8.50</PRICE> 
  64. <YEAR>1990</YEAR> 
  65. </CD> 
  66. <CD> 
  67. <TITLE>Romanza</TITLE> 
  68. <ARTIST>Andrea Bocelli</ARTIST> 
  69. <COUNTRY>EU</COUNTRY> 
  70. <COMPANY>Polydor</COMPANY> 
  71. <PRICE>10.80</PRICE> 
  72. <YEAR>1996</YEAR> 
  73. </CD> 
  74. <CD> 
  75. <TITLE>When a man loves a woman</TITLE> 
  76. <ARTIST>Percy Sledge</ARTIST> 
  77. <COUNTRY>USA</COUNTRY> 
  78. <COMPANY>Atlantic</COMPANY> 
  79. <PRICE>8.70</PRICE> 
  80. <YEAR>1987</YEAR> 
  81. </CD> 
  82. <CD> 
  83. <TITLE>Black angel</TITLE> 
  84. <ARTIST>Savage Rose</ARTIST> 
  85. <COUNTRY>EU</COUNTRY> 
  86. <COMPANY>Mega</COMPANY> 
  87. <PRICE>10.90</PRICE> 
  88. <YEAR>1995</YEAR> 
  89. </CD> 
  90. <CD> 
  91. <TITLE>1999 Grammy Nominees</TITLE> 
  92. <ARTIST>Many</ARTIST> 
  93. <COUNTRY>USA</COUNTRY> 
  94. <COMPANY>Grammy</COMPANY> 
  95. <PRICE>10.20</PRICE> 
  96. <YEAR>1999</YEAR> 
  97. </CD> 
  98. <CD> 
  99. <TITLE>For the good times</TITLE> 
  100. <ARTIST>Kenny Rogers</ARTIST> 
  101. <COUNTRY>UK</COUNTRY> 
  102. <COMPANY>Mucik Master</COMPANY> 
  103. <PRICE>8.70</PRICE> 
  104. <YEAR>1995</YEAR> 
  105. </CD> 
  106. <CD> 
  107. <TITLE>Big Willie style</TITLE> 
  108. <ARTIST>Will Smith</ARTIST> 
  109. <COUNTRY>USA</COUNTRY> 
  110. <COMPANY>Columbia</COMPANY> 
  111. <PRICE>9.90</PRICE> 
  112. <YEAR>1997</YEAR> 
  113. </CD> 
  114. <CD> 
  115. <TITLE>Tupelo Honey</TITLE> 
  116. <ARTIST>Van Morrison</ARTIST> 
  117. <COUNTRY>UK</COUNTRY> 
  118. <COMPANY>Polydor</COMPANY> 
  119. <PRICE>8.20</PRICE> 
  120. <YEAR>1971</YEAR> 
  121. </CD> 
  122. <CD> 
  123. <TITLE>Soulsville</TITLE> 
  124. <ARTIST>Jorn Hoel</ARTIST> 
  125. <COUNTRY>Norway</COUNTRY> 
  126. <COMPANY>WEA</COMPANY> 
  127. <PRICE>7.90</PRICE> 
  128. <YEAR>1996</YEAR> 
  129. </CD> 
  130. <CD> 
  131. <TITLE>The very best of</TITLE> 
  132. <ARTIST>Cat Stevens</ARTIST> 
  133. <COUNTRY>UK</COUNTRY> 
  134. <COMPANY>Island</COMPANY> 
  135. <PRICE>8.90</PRICE> 
  136. <YEAR>1990</YEAR> 
  137. </CD> 
  138. <CD> 
  139. <TITLE>Stop</TITLE> 
  140. <ARTIST>Sam Brown</ARTIST> 
  141. <COUNTRY>UK</COUNTRY> 
  142. <COMPANY>A and M</COMPANY> 
  143. <PRICE>8.90</PRICE> 
  144. <YEAR>1988</YEAR> 
  145. </CD> 
  146. <CD> 
  147. <TITLE>Bridge of Spies</TITLE> 
  148. <ARTIST>T'Pau</ARTIST> 
  149. <COUNTRY>UK</COUNTRY> 
  150. <COMPANY>Siren</COMPANY> 
  151. <PRICE>7.90</PRICE> 
  152. <YEAR>1987</YEAR> 
  153. </CD> 
  154. <CD> 
  155. <TITLE>Private Dancer</TITLE> 
  156. <ARTIST>Tina Turner</ARTIST> 
  157. <COUNTRY>UK</COUNTRY> 
  158. <COMPANY>Capitol</COMPANY> 
  159. <PRICE>8.90</PRICE> 
  160. <YEAR>1983</YEAR> 
  161. </CD> 
  162. <CD> 
  163. <TITLE>Midt om natten</TITLE> 
  164. <ARTIST>Kim Larsen</ARTIST> 
  165. <COUNTRY>EU</COUNTRY> 
  166. <COMPANY>Medley</COMPANY> 
  167. <PRICE>7.80</PRICE> 
  168. <YEAR>1983</YEAR> 
  169. </CD> 
  170. <CD> 
  171. <TITLE>Pavarotti Gala Concert</TITLE> 
  172. <ARTIST>Luciano Pavarotti</ARTIST> 
  173. <COUNTRY>UK</COUNTRY> 
  174. <COMPANY>DECCA</COMPANY> 
  175. <PRICE>9.90</PRICE> 
  176. <YEAR>1991</YEAR> 
  177. </CD> 
  178. <CD> 
  179. <TITLE>The dock of the bay</TITLE> 
  180. <ARTIST>Otis Redding</ARTIST> 
  181. <COUNTRY>USA</COUNTRY> 
  182. <COMPANY>Atlantic</COMPANY> 
  183. <PRICE>7.90</PRICE> 
  184. <YEAR>1987</YEAR> 
  185. </CD> 
  186. <CD> 
  187. <TITLE>Picture book</TITLE> 
  188. <ARTIST>Simply Red</ARTIST> 
  189. <COUNTRY>EU</COUNTRY> 
  190. <COMPANY>Elektra</COMPANY> 
  191. <PRICE>7.20</PRICE> 
  192. <YEAR>1985</YEAR> 
  193. </CD> 
  194. <CD> 
  195. <TITLE>Red</TITLE> 
  196. <ARTIST>The Communards</ARTIST> 
  197. <COUNTRY>UK</COUNTRY> 
  198. <COMPANY>London</COMPANY> 
  199. <PRICE>7.80</PRICE> 
  200. <YEAR>1987</YEAR> 
  201. </CD> 
  202. <CD> 
  203. <TITLE>Unchain my heart</TITLE> 
  204. <ARTIST>Joe Cocker</ARTIST> 
  205. <COUNTRY>USA</COUNTRY> 
  206. <COMPANY>EMI</COMPANY> 
  207. <PRICE>8.20</PRICE> 
  208. <YEAR>1987</YEAR> 
  209. </CD> 
  210. </CATALOG> 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表