Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Inserting text fetched from an XML doc

    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.

    Code:
    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…

    Code:
    <!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>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    try:

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

    instead of

    player.firstChild.nodeValue = getElementTextNS("", "title", items[i], 0);
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Thanks

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Kor
    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...#ID-1950641247>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    aha... if so, why there are two different methods for changing the text into a childNode ?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    aha, ok, thanks.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •