Since a lot of API responses are in XML, we'll review XML parsing in Processing to help you get to the interesting data faster.
1 Log in to Temboo and go to the Yahoo > Weather > GetWeatherByAddress Choreo in our Library.
2 Enter a location in the Address input and click Generate Code to test the Choreo from our website.
3 You get a whole bunch of XML in the Response output with lots of weather-related information about the location. Click the small COPY button, paste the XML into an empty text file, and save as "response.xml".
4 Create a new Sketch in Processing and copy in the code below. Save the sketch and place the "response.xml" file that you created earlier into the same folder as your new sketch. In steps 5-8, we'll go over what happens in the code.
// Declare some global variables XML response; String sunrise, sunset; void setup() { // Load the XML file response = loadXML("response.xml"); // Parse down to what you want XML parsed = response.getChild("channel/yweather:astronomy"); // Put the sunrise and sunset times in strings sunrise = parsed.getString("sunrise"); sunset = parsed.getString("sunset"); // Print the results println(sunrise); println(sunset); }
5 First we have to load the XML data. You could either pass it directly from a Choreo response using parseXML(data) or load it as a file with loadXML(fileName). Since we already saved the XML file in Step 3, we'll go with the second option in this tutorial.
6 Next we need to parse out the data we want from the XML file. It helps to look at the XML file's structure to get an idea of how it is organized. We want the sunrise and sunset attributes. You can find them in <channel><yweather:astronomy>. To get there, use getChild("channel/yweather:astronomy").
7 Finally, you have to actually pull the content from the XML object. For our example we'll use getString().
(Learn about other XML get methods.)
8All Finished! Run the sketch to try it out. You should see the sunrise and sunset times printed in the console.
You should be ready to tackle all sorts of XML parsing now. Check out this example to see how to use a city's current temperature (obtained from same Yahoo Weather XML response) to affect a sketch's background color.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
Processing has additional documentation of their XML methods here. We're always happy to help too. Just email us at support@temboo.com, and we'll answer your questions.
Like what we do? Take a look at our open positions.