Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Grab Headlines From A Remote RSS File

By Nicholas Chase
2003-12-19


Choosing A Version

As long as you're allowing for different formats, you can actually create a system that checks for the feed version before processing it. After all, only RSS 1.0 and 2.0 feeds can have RDF elements, so there's no need to process other feeds. But how can you tell what version to apply?

To solve this problem, you can load the actual feed, analyze it, and use the information to set the proper stylesheet.

Listing 9. Choosing a stylesheet
...

import org.xml.sax.InputSource;
import org.w3c.dom.Element;


public class RSSProcessor {
...
public void setRSSFile(String fileName){

try {

InputSource docFile = new InputSource (fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document inputDoc = db.parse(docFile);
Element rss = inputDoc.getDocumentElement();
String version = null;
if (rss.getNodeName().equals("rss")){
version = rss.getAttribute("version");
if (version == null) {
version = "0.91";
}
} else if (rss.getNodeName().equals("feed")){
version = "echo";
}


String XSLSheetName = version+".xsl";
StreamSource style = new StreamSource(XSLSheetName);

DOMSource interimSource = new DOMSource(inputDoc);

Document interimDoc = db.newDocument();
DOMResult interimResult = new DOMResult(interimDoc);

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer interimTransformer = null;
if (version.equals("0.91")){
interimTransformer = transFactory.newTransformer();
} else {
interimTransformer = transFactory.newTransformer(style);
}

interimTransformer.transform(interimSource, interimResult);


DOMSource source = new DOMSource(interimDoc);
StreamSource finalStyle = new StreamSource("final.xsl");

String outputURL = "headlines.html";
StreamResult result = new StreamResult(new
FileOutputStream(outputURL));

Transformer transformer = transFactory.newTransformer(finalStyle);
transformer.transform(source, result);

} catch (Exception e) {
e.printStackTrace();

}
}

}


In this case, you're loading the feed and checking it for the RSS version, and then using the version number as the file name. The advantage here is that should a new version of RSS be released, you can extend the application by simply adding a new stylesheet. Notice that I've added a check for Echo, or Atom, or whatever RSS's competitor might eventually be called, and that you can also adjust support for it as it changes by simply changing the echo.xsl stylesheet.

The advantage here is that this interim stylesheet is completely generic. A "2.0 - .91" stylesheet will work for anyone, anywhere, and you can make changes to the final output by simply editing final.xsl, whether you support one version or a hundred.

The final.xsl stylesheet is designed for a simple 0.91-style feed, so if you're dealing with one, you'll omit the stylesheet on the interim transformation. This creates an identity transform, in which the document is simply passed along as-is.

That takes care of the problem of multiple versions, but you have one more issue to deal with: concurrency.

Tutorial Pages:
» Retrieve Syndicated Content, Transform It, & Display The Result
» The Source File
» The Primary Stylesheet
» The Basic JSP Page
» Transforming The File
» Adjusting For Multiple Formats
» Choosing A Version
» Caching The Feed
» Conclusion
» Resources


First published by IBM developerWorks


Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business