Parse a RSS XML fileTag(s): XML
In a previous HowTo, we saw how to create a RSS feed using commons.digester or ROME packages. In this HowTo, we are parsing a given feed using the packages.
Parsing a feed with Apache Commons Digester
NOTE: See this HowTo for the required jars.import org.apache.commons.digester.rss.Channel; import org.apache.commons.digester.rss.Item; import org.apache.commons.digester.rss.RSSDigester; import java.net.HttpURLConnection; import java.net.URL; public class ParseRSSFeedUsingCommons { public static void main(String args[]) throws Exception { RSSDigester digester=new RSSDigester(); String feed = "http://www.rgagnon.com/feed.xml"; URL url=new URL(feed); HttpURLConnection httpSource= (HttpURLConnection)url.openConnection(); Channel channel= (Channel)digester.parse(httpSource.getInputStream()); if (channel==null) { throw new Exception("can't communicate with " + url); } Item rssItems[]=channel.findItems(); for (int i=0;i<rssItems.length;i++) { System.out.println(rssItems[i].getTitle()); System.out.println(rssItems[i].getLink()); System.out.println(rssItems[i].getDescription()); System.out.println(); } /* to parse from a file instead of a URL import java.io.FileInputStream; ... String feed = "feed.xml" FileInputStream fis = new FileInputStream(feed); Channel channel=(Channel)digester.parse(fis); ... */ } }
JS: Resize an IFRAME based on its content http://www.rgagnon.com/jsdetails/js-0129.html Using the onLoad event, resize the IFRAME according to its content Java: Communicate between threads using a Queue http://www.rgagnon.com/javadetails/java-0555.html BlockingQueue (Java 5) is designed to work in a multi-threaded world JDBC: Escape special character in a LIKE clause http://www.rgagnon.com/javadetails/java-0554.html How to use LIKE clause wildcards as literals ...
Parsing a feed with ROME
NOTE: See this HowTo for the required jars.import com.sun.syndication.feed.synd.*; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.XmlReader; import java.net.URL; import java.util.List; import java.util.Iterator; public class ParseRSSFeedUsingRome { public static void main(String args[]) throws Exception { String feed = "http://www.rgagnon.com/feed.xml"; URL feedUrl = new URL(feed); SyndFeedInput input = new SyndFeedInput(); SyndFeed sf = input.build(new XmlReader(feedUrl)); List entries = sf.getEntries(); Iterator it = entries.iterator(); while (it.hasNext()) { SyndEntry entry = (SyndEntry)it.next(); System.out.println(entry.getTitle()); System.out.println(entry.getLink()); SyndContent description = entry.getDescription(); System.out.println(description.getValue()); System.out.println(); } /* to parse from a file instead of a URL import java.io.FileInputStream; ... String feed = "feed.xml" FileInputStream fis = new FileInputStream(feed); SyndFeedInput input = new SyndFeedInput(); SyndFeed sf = input.build(new XmlReader(fis)); */ } }