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>
相關文章
- JavaScript讀取文字檔案內容程式碼例項JavaScript
- 簡單介紹正規表示式拆分url例項程式碼
- ajax簡單介紹
- 檔案管理簡單介紹
- php獲取xml檔案內容PHPXML
- 人臉識別的簡要介紹(附例項、Python程式碼)Python
- canvas載入效果程式碼例項Canvas
- C# XML基礎入門(XML檔案內容增刪改查清)C#XML
- 簡單介紹python程式設計之檔案讀寫Python程式設計
- XML節點自動生成簡單例項XML單例
- httprunner3原始碼解讀(1)簡單介紹原始碼模組內容HTTP原始碼
- 簡單介紹python深度學習tensorflow例項資料下載與讀取Python深度學習
- jQuery點選文字框清除內容程式碼例項jQuery
- SVG程式碼構成簡單介紹SVG
- 淡入淡出效果簡單程式碼例項
- 修改所有xml檔案中的某些內容XML
- ajax與XML檔案互動XML
- opengl簡單入門例項
- 簡單介紹Linux環境變數檔案Linux變數
- http 請求-01-AJAX(Asynchronous JavaScript and XML)入門介紹, ajax 的優缺點HTTPJavaScriptXML
- 把list集合的內容寫入到Xml中,通過XmlDocument方式寫入Xml檔案中XML
- 簡單介紹Go 語言單例模式Go單例模式
- Java 例項 - 檔案寫入Java
- python實現修改xml檔案內容詳解PythonXML
- 簡單介紹一下 Linux 檔案系統?Linux
- 簡單介紹Shell指令碼之檔案批次建立與修改的方法指令碼
- 簡單介紹ASP.NET Core實現檔案上傳和下載ASP.NET
- ASP.NET Web Forms – XML 檔案簡介ASP.NETWebORMXML
- 簡單介紹Python drop方法刪除列之inplace引數例項Python
- html實現簡單ListViews效果的例項程式碼HTMLView
- PHP+Ajax點選載入更多列表資料例項PHP
- 簡單介紹python如何在檔案中部插入資訊Python
- 簡單介紹nacos單機本地配置檔案儲存位置方式
- ARouter簡單入門和介紹
- Python訪問小程式簡單方法程式碼例項詳解Python
- java檔案相關(檔案追加內容、檔案內容清空、檔案內容讀取)Java
- 簡單的介紹伺服器和Ajax的應用伺服器
- Ajax 下載檔案
- python操作檔案寫入內容Python