diff --git a/chp18_data/example_18_06_XMLYahooWeather/example_18_06_XMLYahooWeather.pde b/chp18_data/example_18_06_XMLYahooWeather/example_18_06_XMLYahooWeather.pde index 2437f4c..c67c124 100644 --- a/chp18_data/example_18_06_XMLYahooWeather/example_18_06_XMLYahooWeather.pde +++ b/chp18_data/example_18_06_XMLYahooWeather/example_18_06_XMLYahooWeather.pde @@ -1,7 +1,7 @@ /** * Loading XML Data - * by Daniel Shiffman. - * + * by Daniel Shiffman. + * * This example demonstrates how to use loadXML() * to retrieve data from an XML document via a URL */ @@ -10,24 +10,35 @@ int temperature = 0; // We're going to store text about the weather String weather = ""; +// We're going to store the location name +String city = ""; -// The zip code we'll check for -String zip = "10003"; +// The woeid (where on earth id) we'll check for. +// Search for "woeid lookup" to find your own. +String woeid = "638242"; + +PFont font; void setup() { - size(200, 200); + size(600, 360); + + font = createFont("Merriweather-Light.ttf", 28); + textFont(font); // The URL for the XML document - String url = "http://xml.weather.yahoo.com/forecastrss?p=" + zip; + String url = "http://query.yahooapis.com/v1/public/yql?format=xml&q=select+*+from+weather.forecast+where+woeid=" + woeid + "+and+u='C'"; + println(url); // Load the XML document XML xml = loadXML(url); - // Grab the element we want - XML forecast = xml.getChild("channel/item/yweather:forecast"); + // Grab the elements we want + XML location = xml.getChild("results/channel/yweather:location"); + XML forecast = xml.getChild("results/channel/item/yweather:condition"); // Get the attributes we want - temperature = forecast.getInt("high"); + city = location.getString("city"); + temperature = forecast.getInt("temp"); weather = forecast.getString("text"); } @@ -36,8 +47,8 @@ void draw() { fill(0); // Display all the stuff we want to display - text("Zip code: " + zip, 10, 50); - text("Today’s high: " + temperature, 10, 70); - text("Forecast: " + weather, 10, 90); -} + text("Location: " + city, width*0.15, height*0.33); + text("Today’s high: " + temperature, width*0.15, height*0.5); + text("Forecast: " + weather, width*0.15, height*0.66); +}