Posts Tagged ‘RSS’

RSS Transformer

Wednesday, January 28th, 2009

This program takes a RSS feed either from a file or a URL, applies a stylesheet (either file or URL) to it and produces html for viewing with a browser. I compiled and ran an example on my mac - see the html attachment below.

There are three parameters:

  1. xml - may be file or url
  2. xsl - may be file or url
  3. html - file that the transformer puts results

There are hundreds of RSS publishers. From CNN to Reuters to NBC to NPR and everything in between. Google ‘RSS News’ and you will see what I mean.

I apologize for the plain presentation. When time permits, I will add styles (css) to the xsl to shine it up for acceptable presentation.

As a quick note, I use this a lot to test xsl before I move them to my J2EE container. Moving them to the container may take up to 15 minutes at my site. It may not sound much, but in general you may have to make several rounds of changes (i.e. debug) before the xsl is correct. So this is a huge savings as the utility runs in less than three seconds. Why not let the browser transform the xsl? You’d be surprised how different the browser transformers are compared to the java one. Just as a side note, I used this utility today several times as I was testing a new xsl. This is a great utility!

Examples:

NPR Topics: News
java Rss http://www.npr.org/rss/rss.php?id=1001 http://csatechconsulting.com/attachments/055_Rss.xsl Rss.html
Reuters: Top News
jara Rss http://feeds.reuters.com/reuters/topNews http://csatechconsulting.com/attachments/055_Rss.xsl Rss.html

BBC Front Page:
java Rss http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml http://csatechconsulting.com/attachments/055_Rss.xsl Rss.html

RSS Tracking FedEx Package:
java Rss http://www.simpletracking.com/RSSTracking.aspx?TrackingNumber=979887377090 http://csatechconsulting.com/attachments/055_Rss.xsl Rss.html

Rss.java

import java.io.*; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.TransformerException; public class Rss { public static void Rss (String xml, String xsl, String out){ try{ Source xmlSource = new StreamSource(xml); Source xslSource = new StreamSource(xsl); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(xslSource); FileOutputStream file = new FileOutputStream(out); transformer.transform(xmlSource, new StreamResult(file)); } catch (TransformerException e){ System.out.println("Transformer error:" + e.getCause()); } catch (Exception e){ System.out.println("Error:" + e.getCause()); } } public static void main(String[] args){ String xml = args[0]; // xml file or url String xsl = args[1]; // xsl file or url String output = args[2]; // html output file Rss(xml, xsl, output); } }

XSL:
http://csatechconsulting.com/attachments/055_Rss.xsl

Output (from Rss.java):
http://csatechconsulting.com/attachments/055_Rss.html