Reputation: 47
hello i want to parse serverlist.xml with javascript to a html website. its working for me with country, hostname, name, map, numplayers and maxplayers, but when i want to parse the content of "players" with
table += x[i].getElementsByTagName("players")[0].childNodes[0].nodeValue;
then i cant see anything more. i guess its because players tags have some more childnodes, but i dont know how to solve this problem. Thanks for any help.
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>
Upvotes: 1
Views: 90
Reputation:
From the discussion in the comments, below is a fully working example with a text-to-xml parser function as well as a concise data-type identifier.
The simplify
function uses the above-mentioned functions to limit code duplication and complications, and converts the XML DOM structure into an object (or list of objects).
Each "node" has 3 aspects that represents an XML-node:
Kind
- the "nodeName" (tag-name)Attr
- the attributesData
- the contentsYou can change these "aspect names" as you see fit.
To test the code, simply copy & paste into a new HTML file, save & open it with your web browser (no web-server needed for this example).
The output of this example below will appear in the in the web-console
; so, just open your web browser's "developer tools" and view the console log.
See the 3 lines of JavaScript in the last <script>
element as instructions on how to use it.
<html>
<head></head>
<body>
<script>
function typeOf(data)
{
var tpof = (({}).toString.call(data).match(/\s([a-zA-Z]+)/)[1].toLowerCase());
tpof = (((tpof == 'element') || (tpof == 'window') || (tpof == 'global')) ? 'object' : tpof);
tpof = (((tpof == 'htmlcollection') || (tpof == 'namednodemap')) ? 'array' : tpof);
return tpof;
}
function parsed(data)
{
var text, list;
if (typeOf(data) == 'string')
{
data = data.trim();
text = data.toLowerCase();
list = {null:null, true:true, false:false};
if (!isNaN(data))
{ return (data * 1); }
if (list[data])
{ return list[data]; }
if ((data.substr(0,1) == '<') && (data.substr(-1,1) == '>'))
{
var pars = new DOMParser();
var dxml = null;
try
{ dxml = pars.parseFromString(data, "application/xml"); }
catch(err)
{ console.log(err); }
if (dxml)
{ return ([].slice.call(dxml.childNodes)); }
}
}
return data;
}
function simplify(dxml)
{
var resl, kind, indx=0;
var list;
if (typeOf(dxml) == 'array')
{
resl = [];
for (var i in dxml)
{
if (!dxml.hasOwnProperty(i) || !dxml[i].nodeName)
{ continue; }
kind = dxml[i].nodeName;
if ((kind == '#text') || (kind == '#comment'))
{ continue; }
resl[indx] = simplify(dxml[i]);
indx++;
}
return resl;
}
resl = {Kind:dxml.nodeName.toLowerCase(), Attr:{}, Data:''};
if (dxml.attributes && (dxml.attributes.length > 0))
{
list = [].slice.call(dxml.attributes);
for (var i in list)
{
if (!list.hasOwnProperty(i) || !list[i].name || !list[i].value)
{ continue; }
resl.Attr[list[i].name] = parsed(list[i].value);
}
}
if (dxml.childElementCount < 1)
{ resl.Data = (dxml.textContent || ''); }
else
{ resl.Data = simplify([].slice.call(dxml.childNodes)); }
return resl;
}
</script>
<script id="xdom" type="text/xmldata">
<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>
</script>
<script>
var text = document.getElementById('xdom').innerHTML;
var xdom = parsed(text);
var tree = simplify(xdom);
console.log(tree);
</script>
</body>
</html>
If you find the code useful, don't forget to "up-vote", thanks ;)
Upvotes: 1