Category Archives: external data

Yahoo Weather RSS

Actionscript:
  1. var temp:Number;
  2. var weatherData:XML;
  3. var yweather:Namespace;
  4.  
  5. var zipcode:String="11211";
  6. var units:String = "f";  // "f" or "c" for Fahrenheit or Celsius
  7.  
  8. var yahooURL:String = "http://weather.yahooapis.com/forecastrss?p=" + zipcode + "&u=" + units;
  9.  
  10. var yahooWeather:URLLoader = new URLLoader();
  11.  
  12. yahooWeather.load(new URLRequest(yahooURL));
  13. yahooWeather.addEventListener(Event.COMPLETE, onLoaded);
  14.  
  15. function onLoaded(evt:Event):void {
  16.  
  17.     weatherData=new XML(evt.target.data);
  18.     yweather = weatherData.namespace("yweather");
  19.    
  20.     temp = weatherData.channel.item.yweather::condition.@temp;
  21.      
  22.     trace(temp);
  23. }

This snippet comes from a student question about how to get the temperature from yahoo weather.

You could use this snippet to get lots of additional information based on a zipcode... like latitude, longitude, humidity, wind chill etc... The trickiest part of this code is the Namespace stuff... which I find generally annoying... probably just because I'm not used to it.

You can read some detailed info about the content contained in the RSS file here.

Also posted in XML | Tagged , , , , , , | Leave a comment