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 & HelpIntegration › Attach RSS feed to sketch
Page Index Toggle Pages: 1
Attach RSS feed to sketch (Read 2251 times)
Attach RSS feed to sketch
Nov 7th, 2007, 8:55pm
 
Hey all, just a note, I love processing and am excited to get more involved in it. Now, I am VERY new at it and have very little experience with coding, so be nice please!!

My question is, I have an RSS feed I built that pulls news articles from Yahoo! News.

The feed is http://service.openkapow.com/lollipop7081/newspoliticalconsp.rss

So I need to connect the articles retrieved from that Feed to my sketch that I built. All I want it to do is open ONE article (title and description, just description, just title, whatever) when the user clicks anywhere in the window. Is this possible? (I can also attach the feed to an .xml file and run it that way if it would be easier to do that from Processing)

Thanks a bunch!
Re: Attach RSS feed to sketch
Reply #1 - Nov 18th, 2007, 6:40am
 
Here's an example I did that loads your XML file and displays the data every time you click.
You can use this code with any RSS feed you have. You just need to change a couple of parameters.

In order for this code to work, you need the proXML library and a font in your data folder. (I used Verdana).

If you have any questions, send me a message!

Hope it helps

Code:

import proxml.*;

String[][] newsArray = new String[0][3]; //2D array which holds the title, link and description

//elements
XMLElement xmlNews;
XMLInOut XMLInOut;

PFont font;

void setup(){
size(screen.width-200, (screen.width-200) * 9/16); //widescreen
background(0);

font = loadFont("Verdana-20.vlw"); //loads the font
textFont(font);
textSize(10);
fill(255);

XMLInOut = new XMLInOut(this);
try{
XMLInOut.loadElement("http://service.openkapow.com/lollipop7081/newspoliticalconsp.rss"); //loads the XML
}
catch(Exception e){
//if loading failed
println("Loading Failed");
}
}

void xmlEvent(XMLElement element){
//this function is ccalled by default when an XML object is loaded
xmlNews = element;
parseXML(); //appelle la fonction qui analyse le fichier XML
}


void draw(){
}

void parseXML(){
XMLElement node, item;
String title, URLlink, description;
int initialNode = 7; //position of the first node we want (first <item>)... remember the first node is actually index number "0"

node = xmlNews.firstChild().firstChild(); //gets to the child we need
int numberOfNodes = node.countChildren(); //counts the number of children

//loops through all the children that interest us
for (int i=initialNode; i < numberOfNodes; i++) {
item = node.getChild(i); //gets the item element at a certain position
title = item.getChild(0).firstChild().toString(); //gets the title
URLlink = item.getChild(1).firstChild().toString(); //gets the URL link
description = item.getChild(2).firstChild().toString(); //gets the description

//add the data to the 2D array
newsArray = (String[][])append(newsArray, new String[]{
title, URLlink, description }
);
}

//your XML data is now stored in the 2D array
showRandomNews();// calls a function to display the text
}

void showRandomNews() {
int value = int(random(newsArray.length));
String textToShow;

textToShow = newsArray[value][0] + "\n"; //adds the title and line break
textToShow += newsArray[value][1] + "\n"; //adds the link and line break
textToShow += newsArray[value][2]; //adds the description

text(textToShow, int(random(width)), int(random(height)));
}

void mousePressed() {
showRandomNews();
}
Re: Attach RSS feed to sketch
Reply #2 - Nov 18th, 2007, 1:41pm
 
You could also use Rome, which is a great library for working with feeds. It would handle irregular feed formatting better than working with raw XML.

I posted an example a while back. I also included a simplified FeedReader class in the unlekkerLib library.
Re: Attach RSS feed to sketch
Reply #3 - Nov 29th, 2007, 7:47pm
 
refactored code:
no URL display,
title and description in text "box:",
updated color scheme,
one item displayed on either mouse or key press


import proxml.*;

String[][] newsArray = new String[0][3]; //2D array which holds the title, link and description

//elements
XMLElement xmlNews;
XMLInOut XMLInOut;

PFont font, tfont;

void setup(){
 size(screen.width-200, (screen.width-200) * 9/16); //widescreen
 background(0);

 tfont = loadFont("Verdana-16.vlw"); //loads the font
 font = loadFont("Verdana-12.vlw");
 fill(255);

 XMLInOut = new XMLInOut(this);
 try{
     XMLInOut.loadElement("http://service.openkapow.com/lollipop7081/newspoliticalconsp.rss"); //loads the XML
     
 }
 catch(Exception e){
   //if loading failed
   println("Loading Failed");
 }
 
}

void xmlEvent(XMLElement element){
 //this function is ccalled by default when an XML object is loaded
 xmlNews = element;
 parseXML(); //appelle la fonction qui analyse le fichier XML
}


void draw(){
}

void parseXML(){
 XMLElement node, item;
 String title, URLlink, description;
 int initialNode = 7; //position of the first node we want (first <item>)... remember the first node is actually index number "0"

 node = xmlNews.firstChild().firstChild(); //gets to the child we need
 int numberOfNodes = node.countChildren(); //counts the number of children
 

   //loops through all the children that interest us
 for (int i=initialNode; i < numberOfNodes; i++) {
   item = node.getChild(i); //gets the item element at a certain position
   title = item.getChild(0).firstChild().toString(); //gets the title
   URLlink = item.getChild(1).firstChild().toString(); //gets the URL link
   description = item.getChild(2).firstChild().toString(); //gets the description

   //add the data to the 2D array
   newsArray = (String[][])append(newsArray, new String[]{
     title, URLlink, description     }
   );
 }

 //your XML data is now stored in the 2D array
 showRandomNews();// calls a function to display the text
}

void showRandomNews() {
 int value = int(random(newsArray.length));
 int tx = int(random(0,width/2));
 int ty = int(random(100,height-150));
 
 
 background(200);
 textFont(tfont);
 stroke(0); noFill(); rect(tx-10,ty-20,460,180);
 
 noStroke();
 fill(150,0,0); text(newsArray[value][0], tx, ty-10, 400, 20);
 fill(255); rect(tx, ty+10, 430, 130);
 fill(0); textFont(font); text(newsArray[value][2], tx+10, ty+20, 400, 130);
}

void mousePressed() {
 showRandomNews();
}

void keyPressed() {
 showRandomNews();
}
Page Index Toggle Pages: 1