SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looping through xml nodes

    been hacking away at this and can't seem to figure it out. following is the xml that i'm looping through (it doesn't have line breaks, etc, just here for viewing:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1" ?> 
    <posts>
    <post>
    <id>1</id> 
    <date>2006-11-27 16:03:59</date> 
    <title>Test Number 1</title> 
    <description>This is a test</description> 
    </post>
    <post>
    <id>2</id> 
    <date>2006-11-27 16:04:10</date> 
    <title>Test Number 2</title> 
    <description>Another test of this system.</description> 
    </post>
    <post>
    <id>3</id> 
    <date>2006-11-27 16:07:39</date> 
    <title>Test Number 3</title> 
    <description>And a final test to see if I can make this work.</description> 
    </post>
    </posts>
    here's my loop code:
    Code:
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
    		//document.getElementById("txtHint").innerHTML=xmlHttp.responseXML.data;
    		
    		xdoc = xmlHttp.responseXML;
    		postcnt = xdoc.getElementsByTagName('post').length;
    		results = 'There are ' + postcnt + ' posts to display.';
    		document.getElementById("result").innerHTML= results;
    		
    		//alert(xdoc.getElementsByTagName('title').item(i).firstChild.data);
    
    		xposts = xdoc.getElementsByTagName("post");
    		alert(xposts.length);
    		
    		for(i=0;i<xposts.length;i++){
    			posts += posts + '<b>(' + i + ')</b>';
    			
    			if(xposts[i].getElementsByTagName('title')[0].firstChild.nodeValue){
    				posts += posts + 'Title: ';
    				posts += posts + xposts[i].getElementsByTagName('title')[0].firstChild.data;
    			}
    			if(xposts[i].getElementsByTagName('description')[0].firstChild.nodeValue){
    				posts += posts + '<br />Description: ';
    				posts += posts + xposts[i].getElementsByTagName('description')[0].firstChild.data;
    			}
    			
    			posts += posts + '<br /><br />';
    		}
    		
    		document.getElementById("posts").innerHTML = posts;
    		
    	}
    which returns this:
    Code:
    undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a testundefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a test
    
    undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a testundefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a test
    
    (1)undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a testundefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a test
    
    undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a testundefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a test
    
    (1)Title: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a testundefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1undefinedundefined(0)undefinedundefined(0)Title: undefinedundefined(0)undefinedundefined(0)Title: Test Number 1
    Description: This is a test
    
    and on and on until this...
    
    (1)Title: Test Number 2
    Description: Another test of this system.
    
    (2)Title: Test Number 3
    Description: And a final test to see if I can make this work.
    what is with the undefined? what's generating those?

  2. #2
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    i changed my for loop to this:
    Code:
    for(i=0;i<xposts.length;i++){
        posts += posts + '<b>(' + xposts[i].getElementsByTagName('id')[0].firstChild.nodeValue + ')</b> '
            + 'Title: ' + xposts[i].getElementsByTagName('title')[0].firstChild.data
    	+ '<br />Description: ' + xposts[i].getElementsByTagName('description')[0].firstChild.data
    	+ '<br /><br />';
    }
    and my output is now this:
    Code:
    (1) Title: Test Number 1
    Description: This is a test
    
    (1) Title: Test Number 1
    Description: This is a test
    
    (2) Title: Test Number 2
    Description: Another test of this system.
    
    (1) Title: Test Number 1
    Description: This is a test
    
    (1) Title: Test Number 1
    Description: This is a test
    
    (2) Title: Test Number 2
    Description: Another test of this system.
    
    (3) Title: Test Number 3
    Description: And a final test to see if I can make this work.
    still not right, but closer. anyone have any ideas?

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Bill,

    Either use:

    posts = posts + ...

    or

    posts += ...

    but not both.

    After you make that change we should get a better idea of what is going on.

  4. #4
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just saw that - i'm stupid... trying to mix VBScript into there. sorry! it's fixed now

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •