ajstarks
YaBB Newbies
Offline
Posts: 24
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(); }