SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Handle Multiple XML item in Ajax for loop

    Hi. I've started using Ajax, DOM with XML.

    Ive been using it to pull data from a database from Mysql thru php to XML.

    I can get single row of data and use DOM to write the XML onto the page.

    Ive tried to now pull in multiple rows using a for() loop but seems not to work. So for example to display a list of news story titles.

    when I comment '//' the for loop out the request works. Im at a loss how I get it working.

    Thanks for your advice in advance.

    Java Request
    Code JavaScript:
    // HANDLE REQUEST
     
     
     
    function handleResponse() {
     
        if((http.readyState == 4)&&(http.status == 200)){
     
    		var xmlDoc = http.responseXML.documentElement;
     
    		var newsHolder = document.getElementById("newsholder");
     
    		for(var i = 0; i < xmlDoc.length; i++){
     
    		var title = document.createElement("h2");
    title.innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    		newsHolder.appendChild(title);
    		}	
    	}
    }

    Below is the general layout of my XML file

    Code XML:
    <?xml version="1.0" encoding="ISO-8859-1"?>
     
    <newslist>
     
      <article>
          <newsid></newsid>
          <author></author>
          <title></title>
          <content></content>
          <lastedited></lastedited>
          <published></published>
       </article>
     
      <article>
          <newsid></newsid>
          <author></author>
          <title></title>
          <content></content>
          <lastedited></lastedited>
          <published></published>
       </article>
     
    </newslist>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    xmlDoc doesn't have a useful length.
    What you want to do is loop through the articles, so you need to obtain those beforehand and use that list instead.

    I presume that you will be wanting to work with more than just the titles.
    If it's only the titles you want though then the below code can be made simpler.

    Code javascript:
    var newsHolder = document.getElementById("newsholder"),
        xmlDoc = http.responseXML.documentElement,
        articles = xmlDoc.getElementsByTagName('article');
    for(var i = 0; i < articles.length; i++){
        var titleValue = articles[i].getElementsByTagName("title")[0].firstChild.nodeValue;
        var title = document.createElement("h2");
        title.appendChild=document.createTextNode(titleValue);
        newsHolder.appendChild(title);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    thanks for that. I can follow the logic of that.

    I ran the page is am building thru firebug

    http://www.acumendesign.co.uk/acumen_cms/news.php

    Click on the submit to test

    Layout in FF needs work

    there are no error in the script and I can see that the responses get sent and are recieved but still nothing is displayed.

    Im really baffled now :P

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Sorry, my bad. The appendChild should be in parens instead of as an assignment.

    Code javascript:
    // title.appendChild=document.createTextNode(titleValue);
    title.appendChild(document.createTextNode(titleValue));
    Last edited by paul_wilkins; Mar 14, 2008 at 08:13.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that works quite well.


Bookmarks

Posting Permissions

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