...

View Full Version : Inserting text fetched from an XML doc



gsnedders
12-15-2005, 04:37 PM
I'm having problems getting the script to work, while I get the list cleared out, and blank <li> element created, it has nothing within it.



function loadXMLDoc(url)
{
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
}

function processReqChange()
{
// only if req shows "loaded"
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
clearPlayers();
buildPlayers();
}
else
{
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}

function getElementTextNS(prefix, local, parentElem, index)
{
var result = "";
result = parentElem.getElementsByTagName(local)[index];
if (result)
{
if (result.childNodes.length > 1)
{
return result.childNodes[1].nodeValue;
}
else
{
return result.firstChild.nodeValue;
}
}
else
{
return "n/a";
}
}

function clearPlayers()
{
var ul = document.getElementById("players");
while (ul.firstChild)
{
ul.removeChild(ul.firstChild);
}
}

function buildPlayers() {
var players = document.getElementById("players");
var items = req.responseXML.getElementsByTagName("item");
for (var i = 0; i < items.length; i++) {
var player = document.createElement("li");
players.insertBefore(player, players.firstChild);
player.firstChild.nodeValue = getElementTextNS("", "title", items[i], 0);
}
}

function load()
{
loadXMLDoc("http://stats.bzflag.org/rss.php?feed=players&server=viper.pimpi.org:5158&order=score");
}

window.onload = load;

…and the XHTML…


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML Example</title>
<script type="text/javascript" src="bzflag.js"></script>
</head>
<body>
<ul id="players">
<li>Blah</li>
</ul>
</body>
</html>

Kor
12-15-2005, 04:49 PM
try:

player.appendChild(document.createTextNode(getElementTextNS("", "title", items[i], 0));

instead of

player.firstChild.nodeValue = getElementTextNS("", "title", items[i], 0);

gsnedders
12-15-2005, 04:55 PM
Thanks :)

Kor
12-15-2005, 05:00 PM
The ideea is that when you create an element (or even is written HTML as <tag></tag>, with not even an empty space) there is no firstChild, there is no child at all.

By the way, I guess that is better to use:

element.firstChild.data
instead of
element.firstChild.nodeValue

even if it works, as far as I know nodeValue is a readonly attribute...

liorean
12-15-2005, 06:23 PM
even if it works, as far as I know nodeValue is a readonly attribute...It isn't. See <uri:http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-1950641247>

Kor
12-16-2005, 08:29 AM
aha... if so, why there are two different methods for changing the text into a childNode ?

liorean
12-16-2005, 09:34 PM
One is generic - a way to access the textual content of any node that has a pure text content mode. One is specific - a way to access the content of a text node.

Kor
12-19-2005, 08:50 AM
aha, ok, thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum