• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Make Your House Talk > Weather Information

4.2. Weather Information

There are many different ways to look up the current weather on the Internet—you probably use a browser to read one of the many web pages showing weather information. Such web pages tend to have a lot of stuff besides the current temperature, though, such as pretty icons illustrating the current weather, a toolbar, and advertisements (see Figure 4-2).

Of course, your brain is smart enough to ignore all of these distractions and focus on the current temperature. That's not so easy for a computer to do. A computer can certainly download the page, but digging through all of the information in the source code to retrieve the current temperature is difficult (see Figure 4-3). It can be done, though, using a technique called screen scraping. Screen scraping downloads a web page and extracts just the information needed by an application.

Figure 4-2. My eyes can easily find the temperature on this page with no problem…

Figure 4-3. …but finding it buried in the HTML is a bit more difficult.

Unfortunately, screen scraping isn't an ideal way for two computers to communicate. It's challenging to program because you have to create a script that digs through raw HTML and looks for some way to consistently determine where in the HTML file the current temperature is located. It's certainly doable, but it's also unreliable—whenever the layout of the page changes, your program will break, and you'll have to figure out the HTML all over again.

But thanks to web services, two computers on the Internet can communicate in a much more computer-friendly way: by using Extensible Markup Language (XML). XML is just a text format that allows computers to exchange a wide variety of information. XML libraries exist for all major development environments now, so you can consume XML information in just about any application.

Here's what the current temperature on the Weather.com site looks like in XML:

	      <ns1:getTempResponse SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/
encoding/" xmlns:ns1="urn:xmethods-Temperature">
			   <return xsi:type="xsd:float">47.0</return>


It's not so easy for a human to read, but for a computer, it's simple. Most of the XML above is dedicated to describing the Simple Object Access Protocol (SOAP) envelope and the XML schema, which shows the client parsing the XML file exactly how to parse it. Note that the current temperature, expressed as a floating-point number, is contained in the tag <return xsi:type="xsd:float">47.0</return>.

Slashdot offers their news articles in both XML and RDF Site Summary (RSS) formats. Check out http://slashdot.org/index.xml and http://slashdot.org/index.rss. It's very simple to grab and parse these using just about any programming environment.

In this first phase of the project, you'll create a small application to retrieve weather information from the Web using SOAP, extract the current temperature, and output the current temperature to the console. Later, you'll grab this output and use it in your speech application.

4.2.1. 1. Create the temperature retrieval application

This step assumes you have Microsoft Visual Studio .NET and the .NET Framework installed. If you don't want to use Visual Studio, you can compile this application using free tools that Microsoft provides. Or, even easier, visit http://www.homehacking.com/ and download the executables.

  1. Launch Microsoft Visual Studio .NET.

  2. Go to File New Project.

  3. In the New Project dialog, click Visual C# Projects, and then click Console Application.

  4. In the Name field, type GetTempFromZip. Click OK.

  5. Click the Project menu, then click Add Web Reference.

  6. Click the Project menu, then click Add Reference.

  7. In the URL field, type http://www.xmethods.net/sd/2001/TemperatureService.wsdl, then click Go.

  8. Visual Studio requests the WSDL file you specified, and then parses it to determine what methods are available and how to communicate with them. It then provides you with a list of methods, as shown in Figure 4-4. Click Add Reference.

    Figure 4-4. Using web services is much easier than screen scraping.

  9. Replace the default Class1.cs code with the following:

    	using System;
    	using System.Diagnostics;
    	using System.Xml.Serialization;
    	using System.Web.Services.Protocols;
    	using System.ComponentModel;
    	using System.Web.Services;
    	namespace GetTempFromZip
    	  class Class1
    		static void Main(string[] args)
    		   net.xmethods.www.TemperatureService tempSvc = new net.xmethods.www.TemperatureS
    		   Console.WriteLine( tempSvc.getTemp( args[0] ) );

  10. On the Standard toolbar, click the list that currently shows Debug, and then click Release. This ensures that you are using the release build configuration.

  11. Click the Build menu, then click Build Solution.

Time to test it out. Open a terminal and switch to the directory your project was configured to build in (by default, this will be C:\Documents and Settings\username\My Documents\Visual Studio Projects\GetTempFromZip\bin\Release). Then execute the following command:

	GetTempFromZip 01801

Your computer will contact the xmethods.net web service, retrieve the current temperature for my ZIP code, output it to the console, and exit. Of course, if you don't care about how warmly I need to dress, you can pass the program your own ZIP code instead.

Web services make it really, really easy to use data from the Web in an application. In fact, there are only two interesting lines of code in this application:

	net.xmethods.www.TemperatureService tempSvc = new net.xmethods. www.TemperatureService
	Console.WriteLine( tempSvc.getTemp( args[0] ) );


These lines create a new TemperatureService object, and then call the getTemp method using the ZIP code. The ZIP code is passed to the application as an argument, which is represented by args[0] (the variable args is declared with the line static void Main(string[] args)). The .NET Framework then performs a web services request in the background and returns the results, which are written to the console.

This could easily be a much longer application—if you wanted, you could provide usage information, comments, and exception handling. In fact, if you don't pass it a parameter, it'll throw an unhandled exception. But hey, it's just a hack.

Be sure that any computer you run this application on has the .NET Framework installed. You can get the .NET Framework from the Recommended Updates section of Windows Update at http://windowsupdate.microsoft.com.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint