ajax載入xml檔案內容程式碼例項簡單介紹
本章節介紹一下如何用原生的ajax載入伺服器上的xml檔案內容。
程式碼非常的簡單,需要的朋友可以做一下參考,程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> function loadXMLDoc(url){ var xmlhttp; var txt,x,xx,i; if(window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>"; x=xmlhttp.responseXML.documentElement.getElementsByTagName("cd"); for (i=0;i<x.length;i++){ txt=txt + "<tr>"; xx=x[i].getElementsByTagName("title"); { try{ txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>"; } catch (er){ txt=txt + "<td> </td>"; } } xx = x[i].getElementsByTagName("artist"); { try{ txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>"; } catch (er){ txt=txt + "<td> </td>"; } } txt=txt + "</tr>"; } txt=txt + "</table>"; document.getElementById('txtCDInfo').innerHTML=txt; } } xmlhttp.open("GET",url,true); xmlhttp.send(); } window.onload=function(){ var obt=document.getElementById("bt"); obt.onclick=function(){ loadXMLDoc('demo/ajax/xml/antzone.xml') } } </script> </head> <body> <div id="txtCDInfo"></div> <input type="button" id="bt" value="檢視效果"/> </body> </html>
上面的程式碼點選按鈕可以實現載入解析效果,xml檔案內容程式碼如下:
[XML] 純文字檢視 複製程式碼<?xml version="1.0" encoding="utf-8"?> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>hide your heart</title> <artist>bonnie tyler</artist> <country>uk</country> <company>cbs records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>greatest hits</title> <artist>dolly parton</artist> <country>usa</country> <company>rca</company> <price>9.90</price> <year>1982</year> </cd> <cd> <title>still got the blues</title> <artist>gary moore</artist> <country>uk</country> <company>virgin records</company> <price>10.20</price> <year>1990</year> </cd> <cd> <title>eros</title> <artist>eros ramazzotti</artist> <country>eu</country> <company>bmg</company> <price>9.90</price> <year>1997</year> </cd> <cd> <title>one night only</title> <artist>bee gees</artist> <country>uk</country> <company>polydor</company> <price>10.90</price> <year>1998</year> </cd> <cd> <title>sylvias mother</title> <artist>dr.hook</artist> <country>uk</country> <company>cbs</company> <price>8.10</price> <year>1973</year> </cd> <cd> <title>maggie may</title> <artist>rod stewart</artist> <country>uk</country> <company>pickwick</company> <price>8.50</price> <year>1990</year> </cd> <cd> <title>romanza</title> <artist>andrea bocelli</artist> <country>eu</country> <company>polydor</company> <price>10.80</price> <year>1996</year> </cd> <cd> <title>when a man loves a woman</title> <artist>percy sledge</artist> <country>usa</country> <company>atlantic</company> <price>8.70</price> <year>1987</year> </cd> <cd> <title>black angel</title> <artist>savage rose</artist> <country>eu</country> <company>mega</company> <price>10.90</price> <year>1995</year> </cd> <cd> <title>1999 grammy nominees</title> <artist>many</artist> <country>usa</country> <company>grammy</company> <price>10.20</price> <year>1999</year> </cd> <cd> <title>for the good times</title> <artist>kenny rogers</artist> <country>uk</country> <company>mucik master</company> <price>8.70</price> <year>1995</year> </cd> <cd> <title>big willie style</title> <artist>will smith</artist> <country>usa</country> <company>columbia</company> <price>9.90</price> <year>1997</year> </cd> <cd> <title>tupelo honey</title> <artist>van morrison</artist> <country>uk</country> <company>polydor</company> <price>8.20</price> <year>1971</year> </cd> <cd> <title>soulsville</title> <artist>jorn hoel</artist> <country>norway</country> <company>wea</company> <price>7.90</price> <year>1996</year> </cd> <cd> <title>the very best of</title> <artist>cat stevens</artist> <country>uk</country> <company>island</company> <price>8.90</price> <year>1990</year> </cd> <cd> <title>stop</title> <artist>sam brown</artist> <country>uk</country> <company>a and m</company> <price>8.90</price> <year>1988</year> </cd> <cd> <title>bridge of spies</title> <artist>t'pau</artist> <country>uk</country> <company>siren</company> <price>7.90</price> <year>1987</year> </cd> <cd> <title>private dancer</title> <artist>tina turner</artist> <country>uk</country> <company>capitol</company> <price>8.90</price> <year>1983</year> </cd> <cd> <title>midt om natten</title> <artist>kim larsen</artist> <country>eu</country> <company>medley</company> <price>7.80</price> <year>1983</year> </cd> <cd> <title>pavarotti gala concert</title> <artist>luciano pavarotti</artist> <country>uk</country> <company>decca</company> <price>9.90</price> <year>1991</year> </cd> <cd> <title>the dock of the bay</title> <artist>otis redding</artist> <country>usa</country> <company>atlantic</company> <price>7.90</price> <year>1987</year> </cd> <cd> <title>picture book</title> <artist>simply red</artist> <country>eu</country> <company>elektra</company> <price>7.20</price> <year>1985</year> </cd> <cd> <title>red</title> <artist>the communards</artist> <country>uk</country> <company>london</company> <price>7.80</price> <year>1987</year> </cd> <cd> <title>unchain my heart</title> <artist>joe cocker</artist> <country>usa</country> <company>emi</company> <price>8.20</price> <year>1987</year> </cd> </catalog>
相關文章
- ajax讀取檔案中內容的程式碼例項
- js解析xml檔案簡單程式碼例項JSXML
- ajax實現跨域請求程式碼例項簡單介紹跨域
- js動態修改元素中的內容程式碼例項簡單介紹JS
- js ajax惰性載入程式碼例項JS
- 動態載入js檔案簡單介紹JS
- 點選載入更多網頁內容效果簡單介紹網頁
- JavaScript讀取文字檔案內容程式碼例項JavaScript
- jQuery解析xml檔案程式碼例項jQueryXML
- jQuery動態載入js檔案簡單介紹jQueryJS
- js動態載入js檔案簡單介紹JS
- javascript讀取xml檔案程式碼例項JavaScriptXML
- jquery圖片預載入簡單程式碼例項jQuery
- $.ajax()用法例項程式碼介紹
- 簡單介紹正規表示式拆分url例項程式碼
- 旋轉等待內容載入完畢實現程式碼例項
- javascript動態載入css檔案程式碼例項JavaScriptCSS
- js載入解析xml檔案程式碼JSXML
- 按照順序動態載入js檔案簡單介紹JS
- jQuery如何處理xml檔案程式碼例項jQueryXML
- js獲取指定單元格的內容程式碼例項例項JS
- jQuery實現的生成隨機密碼程式碼例項簡單介紹jQuery隨機密碼
- 動態載入js或者css檔案程式碼例項JSCSS
- 實現js檔案動態載入程式碼例項JS
- js實現的動態載入css檔案簡單介紹JSCSS
- jquery實現的解析xml檔案程式碼例項jQueryXML
- js獲取並解析xml檔案程式碼例項JSXML
- 檔案管理簡單介紹
- jQuery構造物件例項簡單介紹jQuery物件
- javascript判斷flash檔案載入完畢程式碼例項JavaScript
- 兩個文字框同步輸入內容程式碼例項
- 實現文字框輸入內容提示程式碼例項
- js 將xml轉換為table表格簡單程式碼例項JSXML
- 使用ajax方式提交表單程式碼例項
- jQuery實現的動態載入指令碼檔案程式碼例項jQuery指令碼
- $.ajax()函式用法簡單例項函式單例
- js讀寫二進位制檔案簡單程式碼例項JS
- 使用<link>標籤引入css檔案簡單程式碼例項CSS