We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › proXML request problem....
Page Index Toggle Pages: 1
proXML request problem.... (Read 472 times)
proXML request problem....
Mar 30th, 2006, 2:06am
 
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);



}



}
Re: proXML request problem....
Reply #1 - Mar 30th, 2006, 9:20pm
 
It's most likely because Yahoo blocks the IPs that continiously access the XML data, to prevent creating unnecessary stress on their server. When you access external feed data that does not change too often (like weather), you have to be careful not to actually pull the file over and over. It's called "playing nice" and alot of sites that provide feed data do implement these kind of checks.
My siggestion would be to keep the data internally after it's loaded once and only access the external file at certain intervals.
Re: proXML request problem....
Reply #2 - Mar 30th, 2006, 9:31pm
 
Thanks for the suggestion!

Do you know of any method that can be done to refresh the file at an interval so yahoo doesn't get angry? I suppose I can use the saveTo() function in the setup() part and then call the internal XML file for the draw part, but the setup method would still be presumably calling the real XML file every second, leading to another lockout?

Thanks for  your help though! I needed that!
Page Index Toggle Pages: 1