// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 18-5: Parsing Yahoo's XML weather feed manually

PFont f;
String[] zips = { "10003" , "21209" , "90210" };
int counter = 0;

// The WeatherGrabber object does the work for us!
WeatherGrabber wg;

void setup() {
  // Make a WeatherGrabber object
  wg = new WeatherGrabber(zips[counter]);
  // Tell it to request the weather
  f = createFont( "Georgia" ,16,true);

void draw() {
  // Get the values to display
  String weather = wg.getWeather();
  int temp = wg.getTemp();
  // Display all the stuff we want to display
  text("Click to change zip. " ,10,180);
  // Draw a little thermometer based on the temperature

void mousePressed() {
  // Increment the counter and get the weather at the next zip code
  counter = (counter + 1) % zips.length;
  // The data is requested again with a new zip code every time the mouse is pressed.
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 18-5: Parsing Yahoo's XML weather feed manually

// A WeatherGrabber class
class WeatherGrabber {
  int temperature = 0;
  String weather = "";
  String zip;
  WeatherGrabber(String tempZip) {
    zip = tempZip;
  // Set a new Zip code
  void setZip(String tempZip) {
    zip = tempZip;
  // Get the temperature
  int getTemp() {
    return temperature;
  // Get the weather
  String getWeather() {
    return weather;
  // Make the actual XML request
  void requestWeather() {
    // Get all the HTML/XML source code into an array of strings
    // (each line is one element in the array)
    String url = "http://xml.weather.yahoo.com/forecastrss?p=" + zip;
    String[] lines = loadStrings(url);
    // Turn array into one long String
    String xml = join(lines, "" ); 
    // Searching for weather condition
    String lookfor = "<yweather:condition  text="";
    String end = """;
    weather = giveMeTextBetween(xml,lookfor,end);
    // Searching for temperature
    lookfor = "temp="";
    temperature = int(giveMeTextBetween (xml,lookfor,end));
  // A function that returns a substring between two substrings
  String giveMeTextBetween(String s, String before, String after) {
    String found = "";
    int start = s.indexOf(before);    // Find the index of the beginning tag
    if (start == - 1) return"";       // If we don't find anything, send back a blank String
    start += before.length();         // Move to the end of the beginning tag
    int end = s.indexOf(after,start); // Find the index of the end tag
    if (end == -1) return"";          // If we don't find the end tag, send back a blank String
    return s.substring(start,end);    // Return the text in between
  • http://www.ccttours.com/blog ww

    I downloaded the source pde file and ran it, everything is OK but no weather condition displayed, why?

  • http://www.learningprocessing.com Daniel Shiffman

    I double-checked the example and it works as far as I can tell. If your internet connection is not active, the weather info won’t display. That’s all I can think of at the moment!


  • http://www.ccttours.com/blog ww

    Of course I’m connected to the internet… But one thing strange, on this page, I can see both temp and weather condition on your example, meanwhile on my macbook, with the same code, I just could see temp but weather condition…

  • http://www.learningprocessing.com Daniel Shiffman

    Whoops, my mistake. Yahoo changed their XML feed slightly. To parse it properly, the line of code needs to read:

        // Searching for weather condition
        String lookfor = "

    I uploaded a fixed version!

  • http://www.ccttours.com/blog ww

    Well, after the very very careful comparison, I finally found, on the “Searching for weather condition” section, one must type two spacebar between “yweather:condition” and “text=”, unless it will never show the weather condition.

  • Alex

    Strange things happen. After trying and trying, I just copied the entire code but STILL get the error message “cannot convert from int to String” for the line “String weather = wg.getWeather();”

  • shiffman

    Sorry, I don’t get this error.  Send me screenshot? What version of Processing?

  • Alex

    Nevermind, downloading the source code works fine. Unfortunately, it seems I didn’t save my latest try, where I was typing every single line of code and spending a full afternoon checking everything against the one posted on here. Even textdiffs and printing them out didn’t help, haha.

    Thanks, though, for still answering questions, Daniel. Your contributions to everything regarding Processing can’t be appreciated enough. (Really looking forward to your Nature of Code book.)

  • T.M

    Does anybody know how I can get data from other countries. This code only works for US zip codes, right?