+ Reply to Thread
Results 1 to 4 of 4

Thread: Javascript Read XML

  1. #1
    dishwasher's Avatar
    dishwasher is offline Toddler dishwasher is on a distinguished road
    Join Date
    Jan 2007
    Posts
    46
    Rep Power
    0

    Javascript Read XML

    Hello,

    I have a small problem with displaying a XML nodeValue. This is my code:

    <html>
    <head>
    <script type="text/javascript">
    var xmlDoc;
    function loadXML()
    {
    // code for IE
    if (window.ActiveXObject)
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load("http://webmail.tabor.nl/werenfridus/Rooster/Dagelijkse_rooster/lk_xml1.xml");
    getmessage();
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation &&
    document.implementation.createDocument)
    {
    xmlDoc=document.implementation.createDocument(""," ",null);
    xmlDoc.load("http://webmail.tabor.nl/werenfridus/Rooster/Dagelijkse_rooster/lk_xml1.xml");
    xmlDoc.onload=getmessage;
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    }
    function getmessage()
    {
    var noitems
    var noitems = xmlDoc.getElementsByTagName("item").length;
    document.write("number of items: ",noitems);
    document.write("<p>")
    var lichtkrant = xmlDoc.getElementsByTagName("lichtkrant61");
    var items = lichtkrant[0].getElementsByTagName("item");
    var x=0
    for (var x=0; x <= noitems; x=x+1)
    {
    var regel = items[x].getElementsByTagName("regel");
    document.write("<p>");
    document.write(regel[0].firstChild.nodeValue);
    document.write("</p>");
    }
    }

    </script>
    </head>
    <body>
    Update me: <a href="#" onClick="loadXML()">Update HIM</a>
    </body>
    </html>

    As you can see my XML file is located here: http://webmail.tabor.nl/werenfridus/Rooster/Dagelijkse_rooster/lk_xml1.xml

    There are 36 items so noitems is 36, that's displayed right. However if I run this code there will only be 2 items displayed, the third nodevalue is empty, but I wonder will my for loop discontinue after it knows that my value is empty? If yes how can I fix this to display all node values?

    Thanks for you time and help,

    TK

  2. #2
    dishwasher's Avatar
    dishwasher is offline Toddler dishwasher is on a distinguished road
    Join Date
    Jan 2007
    Posts
    46
    Rep Power
    0

    Re: Javascript Read XML

    Nevermind this problem is fixed...

  3. #3
    Calamitie's Avatar
    Calamitie is offline Banned Calamitie will become famous soon enough
    Join Date
    Feb 2005
    Location
    Moodles, UK
    Posts
    2,258
    Rep Power
    0

    Re: Javascript Read XML

    Quote dishwasher originally posted: View Post
    Nevermind this problem is fixed...
    Do you mind posting the solution so others won't have to ask a related question again? Thanks

  4. #4
    dishwasher's Avatar
    dishwasher is offline Toddler dishwasher is on a distinguished road
    Join Date
    Jan 2007
    Posts
    46
    Rep Power
    0

    Re: Javascript Read XML

    Oh sorry about that... the solution is:

    Code:
    function getmessage()
    {
    var noitems
    var noitems = xmlDoc.getElementsByTagName("item").length;
    //document.write("number of items: ",noitems);
    //document.write("<p>")
    var lichtkrant = xmlDoc.getElementsByTagName("lichtkrant61");
    var items = lichtkrant[0].getElementsByTagName("item");
     
    var x=0
    for (var x=0; x <= noitems; x=x+1)
    {
    regel = items[x].getElementsByTagName("regel") 
    try { document.write(regel[0].firstChild.nodeValue)}
    catch(err) { document.write("<br>") }
    document.write("<br>")
    }
    }
    </script>

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Javascript
    By YoungCoder in forum JavaScript Coding
    Replies: 5
    Last Post: 09-25-2004, 04:16 AM
  2. How to Start Javascript
    By Vio-Bear in forum Javascript Articles
    Replies: 0
    Last Post: 09-05-2004, 09:14 AM
  3. Javascript part 1
    By hayabusa in forum Javascript Articles
    Replies: 0
    Last Post: 09-05-2004, 09:01 AM

Posting Permissions

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