import proxml.*; // Import this to use the proxml library PFont f; String elementToFind = "yweather:condition"; // The name of the element that we are looking for. Find this by looking at the XML in a browser (perhaps using "view source") String attributeToFind = "temp"; // The attribute that we want from the above element // From Dan S.'s weather example String weather = ""; String[] zips = {"10003","21209","90210"}; int temp = -1000; // Default temp in case we don't find what we are looking for int counter = 0; void setup() { size(200,200); // Our "getWeather()" function "throws" an error/exception that we need to "catch" try { getWeather(zips[counter]); } catch (Exception e) { println(e); // If an error occurs, print it out } f = loadFont("Georgia-Bold-16.vlw"); println(temp); } void draw() { background(100); textFont(f); textMode(SCREEN); fill(255); text(zips[counter],10,160); text(weather,10,180); text(temp,10,40); noStroke(); fill(200); rect(10,50,temp*2+random(0,5),20); } void mousePressed() { counter = (counter + 1) % zips.length; try { getWeather(zips[counter]); } catch (Exception e) { println(e); } } // Our function that looks for the element that we want: elementName. It returns that element as an XMLElement XMLElement findElement(XMLElement currElement, String elementName) { // Print out the currentElement name for testing println(currElement.getElement()); // Create a default element to return, call it "NOTFOUND" just in case XMLElement foundElement = new XMLElement("NOTFOUND"); // See if the currentElement equals what we are looking for if (currElement.getElement().equals(elementName)) { // if it does, set foundElement to it foundElement = currElement; } else if (currElement.hasChildren()) { // Otherwise, check to see if our currentElement has sub elements // Get the array of childElements XMLElement[] childElements = currElement.getChildren(); // Loop through the array of childElements for (int i = 0; i < childElements.length && foundElement.getElement().equals("NOTFOUND"); i++) { // "Recursively" call this function again for each childElement in the array, again looking for our elementName foundElement = findElement(childElements[i], elementName); } } // Return our foundElement, this will either be the element we are looking for or the "NOTFOUND" element return foundElement; } void getWeather(String zip) throws InvalidDocumentException { // Using our proxy to load the XML url with the zip code we are currently searching for String url = "http://itp.nyu.edu/icm/proxy/proxy.php?url=http://xml.weather.yahoo.com/forecastrss?p=" + zip; // Create an object to load the XML XMLInOut xmlInOut = new XMLInOut(this); // Load the XML tree as an element XMLElement xmlElement = xmlInOut.loadElementFrom(url); // Printing out the name, in this case it is an RSS feed println(xmlElement.getElement()); // Search for the element we are looking for: elementToFind XMLElement foundElement = findElement(xmlElement, elementToFind); // Test to see if what we found is what we are looking for (not "NOTFOUND") if (foundElement.getElement().equals(elementToFind)) { // Check to see if our element has the attribute we are looking for: "temp" if (foundElement.hasAttribute(attributeToFind)) { // Get the value of that attribute temp = foundElement.getIntAttribute(attributeToFind); } } }