Goal: Print all the headlines in an RSS feed
Requires XOM 1.1
import java.io.IOException;
import nu.xom.*;
public class XPathHeadlines {
public static void main(String[] args) {
String url = "http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml";
if (args.length > 0) {
url = args[0];
}
try {
Builder parser = new Builder();
Document doc = parser.build(url);
Nodes titles = doc.query("//title");
for (int i = 0; i < titles.size(); i++) {
System.out.println(titles.get(i).getValue());
}
}
catch (ParsingException ex) {
System.out.println(url + " is not well-formed.");
System.out.println(ex.getMessage());
}
catch (IOException ex) {
System.out.println(
"Due to an IOException, the parser could not read " + url
);
}
}
}