.
Developer Spot - Web Development Tutorials
arrowDeverloper Spot  Tutorials  XML  Grab Headlines From A Remote RSS File 
 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Web Development News
PHP Manual
Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Grab Headlines From A Remote RSS File

By Nicholas Chase
2003-12-19
Reader Rating: 4 out of 5
Bookmark Print Version
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.


Article 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


 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Better SOAP Interfaces With Header Elements

» Variable Substitution In XML Documents

» Create JPEGs Automatically With SVG

» Tip: Convert from HTML to XML with HTML Tidy



 
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML
More Resources: Web Hosting Articles - Web Development News - PHP Manual