Skip to content

Instantly share code, notes, and snippets.

@slambert
Created April 20, 2017 15:42
Show Gist options
  • Save slambert/79d98c3553cbd672f22f3ba76229b979 to your computer and use it in GitHub Desktop.
Save slambert/79d98c3553cbd672f22f3ba76229b979 to your computer and use it in GitHub Desktop.
This is a very short Processing.org program that simply pulls news headlines from the NYT and puts them into an array.
/*
XML exploration v.01
Steve Lambert
April 20, 2017
This is a very short program that simply pulls news headlines from the NYT and puts them
into an array.
I'd never done this before yesterday. Granted, I had some previous knowledge of RSS/XML,
but I decided just to see how far I could get in 15 minutes of research and experimenting
and then see where I was at. In the end it took another 10 before I had what I wanted.
I gleaned from these sources:
1. The Processing Reference on XML
https://processing.org/reference/XML.html
https://processing.org/reference/XML_getChildren_.html
https://processing.org/reference/XML_getChild_.html
2. Shiffman on YouTube - 13.6: XML and JSON - Processing Tutorial
This is, as always, a good walkthrough of some of the basics. Go slow.
https://www.youtube.com/watch?v=rqROpUNb2aY
LET'S DO IT!
*/
// These are our variables
// We're making a variable for all the text in the XML feed
XML xml;
// and this is the URL we're pulling from.
String url = "http://rss.nytimes.com/services/xml/rss/nyt/Golf.xml";
/* Most sites have RSS/XML feeds. Here's a list of all the NYT ones, for example:
http://www.nytimes.com/services/xml/rss/index.html
Another example, my personal site's is:
https://visitsteve.com/feed/
And here's one for a Tumblr site:
http://liartownusa.tumblr.com/rss
*/
void setup() {
xml = loadXML(url); // go get the file from the internet
XML[] items = xml.getChildren("channel/item/title");
/* the line above goes through the all the text in the XML file, finds all the <title>
tags that are nested under <item> tags, that are nested under the <channel> tag. So
to create a condensed example of what might appear in the actual file...
<channel>
<item>
<title>
On Golf: For L.P.G.A. Commissioner Mike Whan, Politics Become Part of the Game
</title>
</item>
<item>
<title>
Review: In ‘Tommy’s Honour,’ a Family Feud on and Off the Green
</title>
</item>
*/
// And the following loop, using getContent() just goes through the array and pulls
// what's between the <title></title> tags and prints it in the console...
for (int i = 0; i < items.length; i++) {
println(items[i].getContent());
}
// From here, you can make magic using live feeds from the internet.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment