CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2006
    Posts
    228

    JavaScript XMLHttpRequest

    Hello,

    I am having problems trying to get a field from a XML file.

    I can load the XML file into my browser and display it as a string but have looked everywhere and can't seem to get a value from the XML file.

    I need to be able to do this client-side (no server-side scripting)

    I am trying to display the current temp etc as a variable
    <w:current temperature="13.5" dewPoint="8.5" humidity="72" windSpeed="7.4" windGusts="9.3" windDirection="WSW" pressure="1019.9" rain="1.6" />


    My JavaScript code looks like the following:
    Code:
    if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       }
     else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     xmlhttp.open("GET",data,false);
     xmlhttp.send();
     xmlDoc=xmlhttp.responseXML;
     
     alert(xmlDoc.getElementsByTagName("w:current")[0].childNodes[0].nodeValue);
    My XML file:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Weather.com.au RSS Feed must be used in accordance with the terms and conditions listed at http://www.weather.com.au/about/rss -->
    <rss version="2.0" xmlns:w="http://rss.weather.com.au/w.dtd">
    	<channel>
    		<title>Weather.com.au - Melbourne Weather</title>
    		<link>http://www.weather.com.au/vic/melbourne</link>
    		<description>Current conditions and forecast for Melbourne, Victoria.</description>
    		<language>en-au</language>
    		<copyright>Copyright 2012 - Weather.com.au Pty Ltd</copyright>
    		<pubDate>Sat, 05 May 2012 10:30:00 GMT</pubDate>
    		<lastBuildDate>Sat, 05 May 2012 10:30:00 GMT</lastBuildDate>
    		<ttl>15</ttl>
    		<item>
    			<title>Melbourne Current Conditions</title>
    			<link>http://www.weather.com.au/vic/melbourne/current</link>
    			<description>
    				<![CDATA[
    					<b>Temperature:</b> 13.5&deg;C<br />
    					<b>Dew Point:</b> 8.5&deg;C<br />
    					<b>Relative Humidity:</b> 72%<br />
    					<b>Wind Speed:</b> 7.4km/h<br />
    					<b>Wind Gusts:</b> 9.3km/h<br />
    					<b>Wind Direction:</b> WSW<br />
    					<b>Pressure:</b> 1019.9hPa<br />
    					<b>Rain Since 9AM:</b> 1.6mm<br />
    				]]>
    			</description>
    			<pubDate>Sat, 05 May 2012 10:30:00 GMT</pubDate>
    			<guid isPermaLink="false">C1336213800</guid>
    			<w:current temperature="13.5" dewPoint="8.5" humidity="72" windSpeed="7.4" windGusts="9.3" windDirection="WSW" pressure="1019.9" rain="1.6" />
    		</item>
    		<item>
    			<title>Melbourne Forecast</title>
    			<link>http://www.weather.com.au/vic/melbourne</link>
    			<description>
    				<![CDATA[
    					<p>
    						<b>Sunday:</b><br />
    						<img src="http://www.weather.com.au/images/icons/4.gif" alt="Partly Cloudy" /><br />
    						Partly Cloudy. Mild.<br />
    						8 - 17<br />
    					</p>
    					<p>
    						<b>Monday:</b><br />
    						<img src="http://www.weather.com.au/images/icons/18.gif" alt="Light Rain Early" /><br />
    						Light Rain Early. Clearing Skies. Mild.<br />
    						8 - 17<br />
    					</p>
    					<p>
    						<b>Tuesday:</b><br />
    						<img src="http://www.weather.com.au/images/icons/1.gif" alt="Sunny" /><br />
    						Sunny. Mild.<br />
    						10 - 22<br />
    					</p>
    				]]>
    			</description>
    			<pubDate>Sat, 05 May 2012 10:30:00 GMT</pubDate>
    			<guid isPermaLink="false">F1336213800</guid>
    			<w:forecast day="Sunday" description="Partly Cloudy. Mild." min="8" max="17" icon="4" iconUri="http://www.weather.com.au/images/icons/4.gif" iconAlt="Partly Cloudy" />
    			<w:forecast day="Monday" description="Light Rain Early. Clearing Skies. Mild." min="8" max="17" icon="18" iconUri="http://www.weather.com.au/images/icons/18.gif" iconAlt="Light Rain Early" />
    			<w:forecast day="Tuesday" description="Sunny. Mild." min="10" max="22" icon="1" iconUri="http://www.weather.com.au/images/icons/1.gif" iconAlt="Sunny" />
    		</item>
    	</channel>
    </rss>

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: JavaScript XMLHttpRequest

    You're missing one very key part...onreadystatechange

    The first "A" in AJAX stands for "asynchronous"...as in when you send() the data, it's not going to reply instantaneously. So, the very next line in your code is not going to be ready to set the xmlDoc variable. That's why you need to check if the readystate has changed before going further.

    http://www.w3schools.com/ajax/ajax_xmlfile.asp
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured