通过ajax在html中显示多个xml信息

时间:2019-01-04 10:59:31

标签: html json ajax xml

我有AirPlayHistory.xml文件,该文件显示最近播放的3首歌曲,并在每次歌曲更改时进行更新。

AirPlayHistory:

<Event status="happened">
<Song title="Forbidden Voices">
<Artist name="Martin Garrix" ID="344518"> </Artist>
<Info StartTime="11:06:31" JazlerID="7235" PlayListerID=""/>
</Song>
<Song title="Faded">
<Artist name="Alan Walker" ID="343769"> </Artist>
<Info StartTime="11:10:28" JazlerID="7769" PlayListerID=""/>
</Song>
<Song title="Afterlife">
<Artist name="Illenium" ID="344414"> </Artist>
<Info StartTime="11:13:30" JazlerID="10668" PlayListerID=""/>
</Song>
</Event>

我有此代码:

var speed = 10000; // 10 seconds

function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e) {try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};

function process(xml) {
document.getElementById('contentfile').innerHTML=xml;
var title = document.getElementById('contentfile').getElementsByTagName('Song')[0].title;
var name = document.getElementById('contentfile').getElementsByTagName('Artist')[0].name;
document.getElementById('contentfile').innerHTML='NOW ON AIR: '+name+' | '+title;
}
function checkXml() {
ajax.get('http://website.com/jazler/NowOnAir.xml',process)
}
window.onload=function() {
checkXml();
tId=setInterval('checkXml()',speed)
}

<div id="contentfile">

但他只显示3首歌曲。

1 个答案:

答案 0 :(得分:0)

清除!您只有第一个写下名称和标题!!您仅重写了innerHTML但代码行document.getElementById('contentfile').innerHTML=xml;不好,您必须循环遍历!现在,您用第一个元素inner.HTMl

写入所有title[0] name[0]的内容