您好,我想用javascript解析serverlist.xml到html網站。它的工作對我來說與國家,主機名,名稱,地圖,numplayers和MAXPLAYERS,但是當我想在html中使用JavaScript解析XML
table += x[i].getElementsByTagName("players")[0].childNodes[0].nodeValue;
解析的「玩家」的內容,那麼我看不到任何東西。我想它是因爲玩家標籤有更多的孩子節點,但我不知道如何解決這個問題。謝謝你的幫助。
serverlist.xml
<qstat>
<server>
<hostname>1.2.3.4:27966</hostname>
<name>Server 1</name>
<gametype>Type 1</gametype>
<map>q3dm3</map>
<numplayers>3</numplayers>
<maxplayers>18</maxplayers>
<numspectators>0</numspectators>
<maxspectators>0</maxspectators>
<ping>0</ping>
<retries>0</retries>
<players>
<player>
<name>E.Krenz^GDR</name>
<score>6</score>
<ping>0</ping>
</player><player>
<name>G.Schroeder^GER</name>
<score>2</score>
<ping>0</ping>
</player>
<player>
<name>W.Ulbricht^GDR</name>
<score>1</score>
<ping>0</ping>
</player></players>
</server>
</qstat>
serverlist.html
<html>
<body>
<table id="demo"></table>
<script>
var x,xmlhttp,xmlDoc
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "../serverlist.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
x = xmlDoc.getElementsByTagName("server");
table='<tr><th bgcolor="#333333">Players</th><th bgcolor="#333333">Country</th><th bgcolor="#333333">Servername</th><th bgcolor="#333333">IP</th><th bgcolor="#333333">Map</th><th bgcolor="#333333">Players</th><th bgcolor="#333333">Connect</th></tr>';
for (i = 0; i <x.length; i++) {
table += "<tr><td>";
table += x[i].getElementsByTagName("players")[0].childNodes[0].nodeValue;
table += "</td><td>";
table += '<iframe src="../serverlist/country/';
table += x[i].getElementsByTagName("hostname")[0].childNodes[0].nodeValue;
table += '.php" height="25px" width="27px" frameborder="0" scrolling="yes"></iframe>'
table += "</td><td>";
table += x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
table += "</td><td>";
table += x[i].getElementsByTagName("hostname")[0].childNodes[0].nodeValue;
table += "</td><td>";
table += x[i].getElementsByTagName("map")[0].childNodes[0].nodeValue;
table += "</td><td>";
table += x[i].getElementsByTagName("numplayers")[0].childNodes[0].nodeValue;
table += "/";
table += x[i].getElementsByTagName("maxplayers")[0].childNodes[0].nodeValue;
table += '</td><td><a href="hlsw://';
table += x[i].getElementsByTagName("hostname")[0].childNodes[0].nodeValue;
table += '/?Connect=1"><img src="../images/hlsw.jpg" width="13" height="13" border="0" />';
table += '<img src="../images/pixel.png" width="10" height="10" border="0" /><a href="qtracker://';
table += x[i].getElementsByTagName("hostname")[0].childNodes[0].nodeValue;
table += '?game=quake3&action=connect"><img src="../images/qtracker.jpg" width="13" height="13" border="0" /></a>'
table += '<img src="../images/pixel.png" width="10" height="10" border="0" /><a href="../serverlist/bat/';
table += x[i].getElementsByTagName("hostname")[0].childNodes[0].nodeValue;
table += '.bat"><img src="../images/bat.png" width="13" height="13" border="0" /></a>'
table += "</td></tr>";
}
document.getElementById("demo").innerHTML = table;
</script>
</body>
</html>
解析XML的DOM節點樹結構,並通過它使用JavaScript的作品有所在某些瀏覽器中有所不同,具體取決於您使用的命令。解決這個問題將消除使用DOM結構時的一些「不可預見的問題」。它可以幫助將XML解析爲對象樹(就像使用JSON一樣) - 之後您可以隨心所欲地遍歷,而不會出現奇怪的問題。我可以通過給你一個很好的「XML DOM to Object」解析器函數來回答這個問題。如果你想嘗試這個,然後回來評論,我會用代碼發佈一個答案。 – argon
感謝您的回答。請發佈您的代碼。謝謝。 ;) – hansfrans