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
    Regular Coder
    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    131
    Thanks
    3
    Thanked 0 Times in 0 Posts

    JS to Parse XML doc

    I'm have trouble writing a proper "for loop" to parse my XML doc and write the info grabbed to my html doc.

    If I simply spell out exactally what I want the info writes as intended; e.g.
    Code:
    document.getElementById("date_1").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;
    document.getElementById("copy_1").innerHTML=xmlDoc.getElementsByTagName("copy")[0].childNodes[0].nodeValue;
    
    document.getElementById("date_2").innerHTML=xmlDoc.getElementsByTagName("date")[1].childNodes[0].nodeValue;
    document.getElementById("copy_2").innerHTML=xmlDoc.getElementsByTagName("copy")[1].childNodes[0].nodeValue;
    But, when I try to consolitdate the above into a loop nothing writes. My XML doc is streight forward; e.g.
    Code:
      <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
    - <news>
    - <entry>
      <date>Entry for 11/8/2006</date> 
      <copy>Some Text Here</copy> 
      </entry>
    - <entry>
      <date>Entry for 11/1/2006</date> 
      <copy>Some Text Here</copy> 
      </entry>
    - <entry>
      <date>Entry for 11/1/2006</date> 
      <copy>Some Text Here</copy> 
      </entry>
      </news>
    And my xhtml is as follows; e.g.
    Code:
    <body onload="loadXML()">
    <p><span id="date_1"></span><br />
    <span id="copy_1"></span>
    </p>
    <p><span id="date_2"></span><br />
    <span id="copy_2"></span>
    </p>
    </body>
    I've tried a number of things but have had no success. Here is what I'm currently working with:
    Code:
    function getMessage()
    {
    	for ( i=0; i < xmlDoc.length; i++ )
    	{
    		var entryDate[i] = xmlDoc.getElementsByTagName("date")[i].childNodes[0].nodeValue;
    		var entryCopy[i] = xmlDoc.getElementsByTagName("copy")[i].childNodes[0].nodeValue;
    
    		document.getElementById("date_" + (1 + i)).innerHTML = entryDate[i];
    		document.getElementById("copy_" + (1 + i)).innerHTML = entryCopy[i];
    	}
    }
    The above is not working. Would anyone have some advice on how to correct?

    Thanks!

  • #2
    Regular Coder
    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    131
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Anyone?

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are saying your first one works and your second doesn't then I'd guess something is wrong in the intermediate variables. I'm not sure what

    var entryDate[i] = ...

    does since it's not really an array but also not a valid variable name (of course javascript seems to allow some wacky code so who knows). Try just

    var entryDate = ...

    If that doesn't work try giving a link to an example that doesn't work because I don't see anything obviously wrong with what you are doing other than the intermediate variable name in your loop.

    david_kw

  • #4
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    hothousegraphix:

    This works, using your .xml file, named: news.xml ---

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	var AdminResponse = "";
    
    	function parseSettings(){
    
    		var nDate = AdminResponse.getElementsByTagName('date');
    		var nCopy = AdminResponse.getElementsByTagName('copy');
    		for (i=1; i<nDate.length+1; i++)
    			{
    			 document.getElementById('date'+i).innerHTML = nDate[i-1].firstChild.data;
    			 document.getElementById('copy'+i).innerHTML = nCopy[i-1].firstChild.data;
    			}
    	}
    
    	function getNews(){
    
    		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
    		AdminRequest.onreadystatechange = function()
    			{
    		 	 if (AdminRequest.readyState == 4)
    				{
    		 	 	 if (AdminRequest.status == 200)
    					{
    			 	 	 AdminResponse = AdminRequest.responseXML;
    			 	 	 parseSettings();
    					}
    		 	 	 else 	{
    				 	 alert('Error news.xml File '+ AdminRequest.statusText);
    					}
    				}
    			}
    		var forceGET = "?n="+ parseInt(Math.random()*999999999);
    		AdminRequest.open("GET", "news.xml"+forceGET, true);
    		AdminRequest.send(null); 
    	}
    
    	onload=getNews;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	
    </style>
    </head>
    	<body>
    		<div id='date1'></div>
    		<div id='copy1'></div>
    
    		<div id='date2'></div>
    		<div id='copy2'></div>
    
    		<div id='date3'></div>
    		<div id='copy3'></div>
    	</body>
    </html>
    Code:
    <?xml version="1.0" ?> 
    	<news>
    		<entry>
      			<date>Entry for 11/8/2006</date> 
      			<copy>Some Text Here</copy> 
      		</entry>
    		<entry>
      			<date>Entry for 11/1/2006</date> 
      			<copy>Some Text Here</copy> 
      		</entry>
    		<entry>
      			<date>Entry for 11/1/2006</date> 
      			<copy>Some Text Here</copy> 
      		</entry>
    	</news>

  • #5
    Regular Coder
    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    131
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry for not responding sooner.

    Ancora - I appreciate you offering a complete solution to my situation. I’ve tested and all works as intended.

    One question, the option here displays the imported content without placing it in the mark-up. Do you have a preference for whether or not the imported information writes to the document?

  • #6
    Regular Coder
    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    131
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry for not responding sooner.

    Ancora - I appreciate you offering a complete solution to my situation. I’ve tested and all works as intended.

    One question, the option here displays the imported content without writing it to the mark-up. Do you have a preference for whether or not the imported information writes to the document?

  • #7
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    hothousegraphix:

    You're welcome. I appreciate your courtesy. It's a rarity here.

    I don't understand. The code I posted is a complete .html document, and when tested with the news.xml document, inserts the relevant text in to the divs.

    I don't know what you mean by:
    "preference for whether or not the imported information writes to the document?"

  • #8
    Regular Coder
    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    131
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ahhh.

    I probably should have been more clear. Yes, the content from news.xml is dynamically inserted in to the div's. However, the script does not write the dynamically inserted text to the page source itself. I'm not at all even sure that there would be a benefit to having that occur.


  •  

    Posting Permissions

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