If anyone can help me, I'd greatly GREATLY appreciate this as it's been driving me mad for days!
Basically, using the external Java Controller (here : http://www.processing.co.il/toolkit/controller/) and proXML I had an idea to grab an XML file from Yahoo Weather, and display the information for the weather from London and New York, with a radio button to swap between the two.
Now the code below works well..... for about five minutes, until the InvalidDocumentException error kicks in and I can't even access Yahoo Weather via the web browser (error 999 - too many connections)
I know that referencing static XML files on the hard drive doesn't create this problem, but perhaps someone would be kind enough to look at the code below and tell me what I'm doing wrong.
(PS You need the controller downloaded for it to work)
Code:
//small xml parser to get weather values using proXML
import proxml.*;
PImage back;
PFont font;
Controller controller;
//xml elements to store weather values
XMLElement channel;
XMLElement item;
XMLInOut xmlInOut;
String UK="UKXX0085";
String USA="USNY0996";
String suffix;
void setup()
{
size(400,400);
smooth();
background(255);
controller = new Controller(this, "togglebuttons:2");
controller.togglebuttons.get(0).setSelected(true);
controller.togglebuttons.setRadioBehavior(true); // gives a "radio" behavior to the button group
}
void controllerUpdated(ControllerEvent e)
{
if (controller.togglebuttons.get(0).isSelected())
{
suffix=UK;
}
else
{
if (controller.togglebuttons.get(1).isSelected())
{
suffix=USA;
}
}
}
void draw()
{
try{
xmlInOut = new XMLInOut(this);
channel = xmlInOut.loadElementFrom("http://xml.weather.yahoo.com/forecastrss?p="+suffix);
background(255);
getWeather();
//delay(5500);
}
catch(InvalidDocumentException ide)
{
font = loadFont("fontA.vlw");
textFont(font, 19);
fill(1);
text("Connection Error",15,20,150,150);
}
}
//show relevant saved in the xml file
void getWeather()
{
//prepare some XMLElements
XMLElement weather;
XMLElement location;
XMLElement units;
XMLElement wind;
XMLElement atmosphere;
XMLElement astronomy;
XMLElement date;
XMLElement latitude;
XMLElement longitude;
for(int i = 0; i < channel.countChildren();i++)
{
//get all the XMLElements needed according to order
//of tags in Yahoo Weather
weather = channel.getChild(i);
date = weather.getChild(4);
location = weather.getChild(6);
units = weather.getChild(7);
wind = weather.getChild(8);
atmosphere = weather.getChild(9);
astronomy = weather.getChild(10);
item = weather.getChild(12);
//Font Loading Sequence
font = loadFont("fontA.vlw");
textFont(font, 10);
fill(1);
int x=15;
int y=30;
//location
text("City : "+location.getAttribute("city")+", "+location.getAttribute("country"),x,y+12);
//temperature and date
text("Currently "+wind.getIntAttribute("chill")+
" degrees Farenheit on "+date.getChild(0),x,y+24);
//coordinates
latitude=item.getChild(1);
longitude=item.getChild(2);
text("Latitude : "+latitude.getChild(0)+" degrees",x,y+36);
text("Longitude : "+longitude.getChild(0)+" degrees",x,y+48);
// controller.arcball.setValue(0.0,,0.0);
//other parameters
text("Wind Direction : "+wind.getIntAttribute("direction")+" degrees",x,y+60);
text("Speed : "+wind.getIntAttribute("speed")+units.getAttribute("speed"),x,y+72);
text("Visibility/Intensity : "+atmosphere.getIntAttribute("visibility")
+units.getAttribute("distance"),x,y+96);
text("Humidity : "+atmosphere.getIntAttribute("humidity"),x,y+108);
text("Pressure : "+atmosphere.getAttribute("pressure")+units.getAttribute("pressure"),x,y+120);
}
}