ptg7068951
Reading RSS Syndication Feeds 307
Period 2
Conditions: partlycloudy
High: 61
Low: 41
Period 3
Conditions: chancerain
High: 67
Low: 43
Period 4
Conditions: chancerain
High: 67
Low: 47
Period 5
Conditions: chancerain
High: 65
Low: 49
Reading RSS Syndication Feeds
There are hundreds of XMLdialects out there representing data in a plat-
form-independent, software-independent manner. One of the most popular
XMLdialects is RSS, a format for sharing headlines and links from online
news sites, weblogs, and other sources of information.
RSS makes web content available in XMLform, perfect for reading in soft-
ware, in web-accessible files called feeds. RSS readers, called news aggrega-
tors, have been adopted by several million information junkies to track all
their favorite websites. There also are web applications that collect and
share RSS items.
The hard-working Builderclass in the nu.xompackage can load XMLover
the Internet from any URL:
String rssUrl = “http://feeds.drudge.com/retort”;
Builder builder = new Builder();
Document doc = builder.build(rssUrl);
This hour’s workshop employs this technique to read an RSS file, present-
ing the 15 most recent items.
Open your editor and enter the text from Listing 21.4. Save the result as
Aggregator.java.
LISTING 21.4 The Full Text of Aggregator.java
1: importjava.io.;
2: importnu.xom.;
3:
4: public classAggregator {
NOTE
The Java classlibrary includes
the Java API for XML Processing
(JAXP),a set of classes that
serve the same purpose as
XOM. JAXP can represent XML
data as an object or a stream
of events,giving a programmer
more control over how the data
is parsed. XOM is easier to
learn and requires properly for-
matted and valid XML atall
times. More information on
JAXP can be found on the Web
at http://jaxp.java.net.