CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: JavaScript XMLHttpRequest

  1. #1
    Join Date
    Mar 2006

    JavaScript XMLHttpRequest


    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:
    if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    My XML file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- RSS Feed must be used in accordance with the terms and conditions listed at -->
    <rss version="2.0" xmlns:w="">
    		<title> - Melbourne Weather</title>
    		<description>Current conditions and forecast for Melbourne, Victoria.</description>
    		<copyright>Copyright 2012 - Pty Ltd</copyright>
    		<pubDate>Sat, 05 May 2012 10:30:00 GMT</pubDate>
    		<lastBuildDate>Sat, 05 May 2012 10:30:00 GMT</lastBuildDate>
    			<title>Melbourne Current Conditions</title>
    					<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 />
    			<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" />
    			<title>Melbourne Forecast</title>
    						<b>Sunday:</b><br />
    						<img src="" alt="Partly Cloudy" /><br />
    						Partly Cloudy. Mild.<br />
    						8 - 17<br />
    						<b>Monday:</b><br />
    						<img src="" alt="Light Rain Early" /><br />
    						Light Rain Early. Clearing Skies. Mild.<br />
    						8 - 17<br />
    						<b>Tuesday:</b><br />
    						<img src="" alt="Sunny" /><br />
    						Sunny. Mild.<br />
    						10 - 22<br />
    			<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="" iconAlt="Partly Cloudy" />
    			<w:forecast day="Monday" description="Light Rain Early. Clearing Skies. Mild." min="8" max="17" icon="18" iconUri="" iconAlt="Light Rain Early" />
    			<w:forecast day="Tuesday" description="Sunny. Mild." min="10" max="22" icon="1" iconUri="" iconAlt="Sunny" />

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002

    Re: JavaScript XMLHttpRequest

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

    The first "A" in AJAX stands for "asynchronous" 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.
    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)