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 & HelpPrograms › iterating past empty rss feeds
Page Index Toggle Pages: 1
iterating past empty rss feeds (Read 638 times)
iterating past empty rss feeds
Jun 16th, 2009, 12:24pm
 
hello,

I am fairly new to programming and working with xml feed.
i am trying to parse through a series of feeds using the xml library and retrieve some data for a visualization project.

String url= "-----------------------------"
                  + X + "-----------";

Some of those feeds are empty.
How do i catch that and move on to the next feed( X++) or any other feed(random X) before i start calling:  
XMLElement report= new XMLElement (this, url);
and  getting a Nested Exception error:
at processing.xml.StdXMLParser.parse(StdXMLParser.java:204)

(The actual parsing of a 'fixed' feed is working fine).

Thanks,

o
Re: iterating past empty rss feeds
Reply #1 - Jun 16th, 2009, 1:27pm
 
You could use a try/catch block and parse it within the try{} section.
Any time you get a Nested Exception (or some kind), increment X inside the catch{} block.
Re: iterating past empty rss feeds
Reply #2 - Jun 16th, 2009, 3:52pm
 
Thank you NoahBuddy.

I have tried a few try and catch block, but i have to say i am not so familiar with them. Here is a stripped down version of the problem i am hoping to solve:  As you press enter, at some point it will crash, therefore the initial question. Maybe you or someone could help me with the following code.
Thank you.
o:o

note: i have taken out "http"  from the url as i am told i need 5 posts before sending a web link.

import processing.xml.*;
PFont f_s, f_m, f_l;
int x=1;

void setup()
{
 size(800, 600);
 f_s= createFont("SanSerif", 12);
 f_m= createFont("SanSerif", 16);
 f_l= createFont("Calibri_bold", 22);
}

void draw()
{
 background(0);
 // try{
 String url= //magicseaweed.com/syndicate/rss/index.php?id="+ x + "&unit=";
 XMLElement report= new XMLElement (this, url);

 //if(report!=null){
 //println(report);
 // }
 // }
 //catch(IOException e){
 //  x++;
 // }

 //////////////////////////////////////////////////////////////////////
 //Get the location // "<link>/Newquay-Fistral-Surf-Report/1/</link>"

 XMLElement Location= report.getChild("channel/title");
// println(Location);
 String locationStg= giveTextInBetween( Location.getContent(), "Latest", "Surf");

 XMLElement reportNum= report.getChild("channel/link");

 String reportNum2= giveTextInBetween( reportNum.getContent(), "Report/", "/");  
 //println(reportNum2);
 String Num= nf(parseInt(reportNum2), 4 );
 // println(Num);
 textFont(f_l);
 fill(255);
 text(Num + ": " + locationStg, 10, 25);

}
//function taken from D.Shiffman
String giveTextInBetween (String s, String startTag, String endTag){
 String found= "";
 int startIndex= s.indexOf(startTag);
 if(startIndex==-1) return "";
 startIndex += startTag.length();
 int endIndex= s.indexOf(endTag, startIndex);
 if(endIndex==-1) return "";
 return s.substring(startIndex, endIndex);
}

void keyPressed(){
 if (key== '\n'){
   x= (int)random(1, 1062);
 }
}



Re: iterating past empty rss feeds
Reply #3 - Jun 16th, 2009, 4:28pm
 
Inside draw():
Code:
...
//Variables need to be declared outside the try/catch block if you use them later
String url = null;
XMLElement report = null;

try{
url= "http://magicseaweed.com/syndicate/rss/index.php?id="+ x + "&unit=";
report = new XMLElement(this, url);
} catch (Exception er) {
er.printStackTrace();//Wha happened?
x++;
return;
}
...

I would also recommend putting the parsing code into the keyPressed method. Not only does it send fewer requests to the address (if that would become a problem), but it will print fewer red error messages when it fails to parse (instead of the default 60 frames per second).

If you are not planning to add animation, you can use noLoop(); inside the setup method, then call redraw() on a key press.
Re: iterating past empty rss feeds
Reply #4 - Jun 16th, 2009, 4:41pm
 
Thanks a lot NoahBuddy!
it's working fine now.

Really appreciated!

o:o
Re: iterating past empty rss feeds
Reply #5 - Jun 16th, 2009, 5:07pm
 
...as well as for your extra tips!

o:o
Page Index Toggle Pages: 1