AJAX XML 实例
AJAX 可用于同 XML 文件进行交互式通信。
AJAX XML 实例
下面的例子演示网页如何通过 AJAX 从 XML 文件读取信息:
实例
<!DOCTYPE html>
<html>
<style>
table,th,td {
border : 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 5px;
}
</style>
<body>
<h1>XMLHttpRequest 对象</h1>
<button type="button" onclick="loadDoc()">获取我的音乐列表</button>
<br><br>
<table id="demo"></table>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "/demo/music_list.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>艺术家</th><th>曲目</th></tr>";
var x = xmlDoc.getElementsByTagName("TRACK");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>
例子解释
当用户点击“获取 CD 信息”按钮时,执行 loadDoc() 函数。
loadDoc() 函数创建 XMLHttpRequest 对象,添加当服务器响应就绪时执行的函数,并向服务器发送请求。
当服务器响应就绪后,构建 HTML 表格,从 XML 文件提取节点(因素),最后使用由 XML 数据填充的 HTML 表格对元素“demo”进行更新。
XML 文件
上例中使用的 XML 文件类似这样:"music_list.xml"。
<CATALOG>
<TRACK>
<TITLE>再见</TITLE>
<ARTIST>邓紫棋</ARTIST>
<ALBUM>新的心跳</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>邓紫棋工作室</COMPANY>
<YEAR>2016</YEAR>
</TRACK>
<TRACK>
<TITLE>All I Ask</TITLE>
<ARTIST>Adele</ARTIST>
<ALBUM>25</ALBUM>
<COUNTRY>英国</COUNTRY>
<COMPANY>XL Recordings</COMPANY>
<YEAR>2015</YEAR>
</TRACK>
<TRACK>
<TITLE>之乎者也</TITLE>
<ARTIST>罗大佑</ARTIST>
<ALBUM>青春舞曲</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>滚石唱片</COMPANY>
<YEAR>1982</YEAR>
</TRACK>
<TRACK>
<TITLE>Never Be Alone</TITLE>
<ARTIST>Shawn Mendes</ARTIST>
<ALBUM>Handwritten</ALBUM>
<COUNTRY>加拿大</COUNTRY>
<COMPANY>环球唱片</COMPANY>
<YEAR>2015</YEAR>
</TRACK>
<TRACK>
<TITLE>慢慢</TITLE>
<ARTIST>张学友</ARTIST>
<ALBUM>忘记你我做不到</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>环球唱片</COMPANY>
<YEAR>1996</YEAR>
</TRACK>
<TRACK>
<TITLE>Complicated</TITLE>
<ARTIST>Avril Lavigne</ARTIST>
<ALBUM>Let Go</ALBUM>
<COUNTRY>加拿大</COUNTRY>
<COMPANY>索尼音乐</COMPANY>
<YEAR>2002</YEAR>
</TRACK>
<TRACK>
<TITLE>三生三世</TITLE>
<ARTIST>张杰</ARTIST>
<ALBUM>三生三世十里桃花</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>仁溪音乐</COMPANY>
<YEAR>2018</YEAR>
</TRACK>
<TRACK>
<TITLE>Five Hundred Miles</TITLE>
<ARTIST>Justin Timberlake</ARTIST>
<ALBUM>Inside Llewyn Davis</ALBUM>
<COUNTRY>美国</COUNTRY>
<COMPANY>华纳唱片</COMPANY>
<YEAR>2013</YEAR>
</TRACK>
<TRACK>
<TITLE>演员</TITLE>
<ARTIST>薛之谦</ARTIST>
<ALBUM>绅士</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>海蝶音乐</COMPANY>
<YEAR>2016</YEAR>
</TRACK>
<TRACK>
<TITLE>Numb</TITLE>
<ARTIST>Linkin Park</ARTIST>
<ALBUM>Meteora</ALBUM>
<COUNTRY>美国</COUNTRY>
<COMPANY>华纳唱片</COMPANY>
<YEAR>2003</YEAR>
</TRACK>
<TRACK>
<TITLE>给未来的自己</TITLE>
<ARTIST>梁静茹</ARTIST>
<ALBUM>崇拜</ALBUM>
<COUNTRY>马来西亚</COUNTRY>
<COMPANY>相信音乐</COMPANY>
<YEAR>2007</YEAR>
</TRACK>
<TRACK>
<TITLE>The Monster</TITLE>
<ARTIST>Rihanna</ARTIST>
<ALBUM>The Marshall Mathers LP2</ALBUM>
<COUNTRY>巴巴多斯</COUNTRY>
<COMPANY>环球唱片</COMPANY>
<YEAR>2013</YEAR>
</TRACK>
<TRACK>
<TITLE>我终于失去了你</TITLE>
<ARTIST>赵传</ARTIST>
<ALBUM>我终于失去了你</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>滚石唱片</COMPANY>
<YEAR>1989</YEAR>
</TRACK>
<TRACK>
<TITLE>Main Titles</TITLE>
<ARTIST>Ramin Djawadi</ARTIST>
<ALBUM>Game of Thrones</ALBUM>
<COUNTRY>德国</COUNTRY>
<COMPANY>索尼音乐</COMPANY>
<YEAR>2011</YEAR>
</TRACK>
<TRACK>
<TITLE>传奇</TITLE>
<ARTIST>李健</ARTIST>
<ALBUM>似水流年</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>北京完美坚持文化艺术工作室</COMPANY>
<YEAR>2003</YEAR>
</TRACK>
<TRACK>
<TITLE>完美生活</TITLE>
<ARTIST>许巍</ARTIST>
<ALBUM>时光漫步</ALBUM>
<COUNTRY>中国</COUNTRY>
<COMPANY>金牌大风</COMPANY>
<YEAR>2002</YEAR>
</TRACK>
</CATALOG>