Vous êtes sur la page 1sur 24

RSS

What is a Feed?
 Syndication feeds – Atom/RSS/RDF feed
 Feed – Small text files - information about
content on websites
 Contents - articles, news, blogs and forums
 When content updated – the feed text file is
also updated
 Programmatically or Manually
History
 1999 - My Netscape portal - single place for
news
 Dan Libby (Netscape) - RDF Site Summary
(RSS) - pull information from different news
sources
 an XML data format based
 later known as RSS 0.9
Contd…
 Dave Winer (Userland Software) -
ScriptingNews (XML based)
 Combined with Libby – launched RSS (Rich
Site Summary) – RSS 0.91
 Netscape continued till 2001
 Winer – declared owner of RSS (RSS 2.0
called as Really Simple Syndication
 RSS 2.0 – News/Blog feeds & Podcasting
What is RSS?
 Designed to show selected data
 RSS feed - not human readable
 Piece of software - RSS aggregator
 Display news - registered with, or
“subscribed to”
 Unread news - appear in bold, just as
unread emails do
Atom Feed
 a lightweight XML format - easy syndication
of web content
 to avoid the limitations and flaws of RSS
 Difference – from Developer’s view – XML
tags used
 Atom is more robust than RSS
 Latter still remains a widely used standard
 Xerox – Photocopy (Eg.)
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

<channel>
<title>Bing: Apple</title>
<link>http://www.bing.com:80/search?q=Apple</link>
<description>Search results</description>
<item>
<title>Apple (India)</title>
<link>http://www.apple.com/in/</link>
<description>Visit website for details…</description>
</item>
<item>
<title>Investor Relations - Apple</title>
<link>http://investor.apple.com/</link>
<description>Apple Inc. business for stockholders,
potential investors, and financial analysts.</description>
</item>
</channel>

</rss>
Useful Websites
 RSS feeds on the fly
http://b2evolution.net/
 Feed Validator
http://www.feedvalidator.org/
 Google Feed API
https://developers.google.com/feed/v1/devguide
 NDTV RSS Feeds
https://www.ndtv.com/rss
Demonstration
<body onload="init()">
<div>
<label>Search String: <input type="text"
id="query"/></label> <br/><br/>
<input type="button" value="Search"
onclick="fetchResults()"/> <br/><br/>
</div> <div id="results"> </div>

function init() {
xhr = new XMLHttpRequest();
query = document.getElementById("query");
searchResults =
document.getElementById("results");
}
function fetchResults()
{
var queries = query.value.split(" ").join("+");
xhr.open("GET", "search.php?query=" + queries,
true);
xhr.onreadystatechange = processResults;
xhr.send();
}
<?php

header("Content-type:text/xml");
extract($_GET);

$query = implode("+", $query));


$url = "http://www.bing.com/search?q=" . $query .
"&format=rss";
echo file_get_contents($url);
?>
function processResults() {
if(xhr.readyState == 4 && xhr.status == 200)
{
root = xhr.responseXML.documentElement;
results = root.getElementsByTagName("item");
populateResults(results);
}
}
function populateResults(results) {
searchResults.innerHTML = "“
for(var i = 0; i<results.length; ++i) {
var newdiv = document.createElement("div");
newdiv.className = "result";
var anchor = createLink(results[i]);
newdiv.appendChild(anchor);
searchResults.appendChild(newdiv);
}
}
function createLink(result)
{
var anchor = document.createElement("a");
var link =
result.getElementsByTagName("link")[0].firstChild.n
odeValue;
var title =
result.getElementsByTagName("title")[0].firstChild.n
odeValue;
anchor.href = link;
anchor.innerHTML = title;
return anchor; }
Description
 <channel> element - to describe the RSS feed
 It has three required child elements:
 <title> - Defines the title of the channel
 <link> - Defines the hyperlink to the channel
 <description> - Describes the channel
 Each <channel> element can have one or more
<item> elements.
 Each <item> element defines an article or "story"
in the RSS feed.
XML based Parsers
XML Parsers
 Process data in a document or data’s
structure (Validate)
 Access or modify data in a document
 All browsers – built-in parsers - convert
text into an XML DOM object
 Commonly used – DOM, SAX, StAX, JAXB
 APIs – JDOM, JiBX
How parser works?
INTERNAL STRUCTURE
Some Well Known XML Parsers
 Microsoft’s XML parser – MSXML
 Apache Xerces
 XML4J (IBM)
 James Clark’s Expat
Java XML Parser – DOM
 DOM interfaces – Node, Element, Attr, Text,
Document
 Common DOM methods –
Document.getDocumentElement(),
Node.getFirstChild(), Node.getLastChild(),
Node.getNextSibling(),
Node.getPreviousSibling(),
Node.getAttribute(attrName)
Steps to using DOM
 Import XML-related packages.
 Create a Document Builder.
 Create a Document from a file or stream
 Extract the root element
 Examine attributes
 Examine sub-elements
SAX (Simple API for XML)
 implements SAX API
 an event based API (parser)
 To parse XML file – implement Handler
class with callback methods
 does not create any internal structure.
 simple and memory efficient.
 very fast and works for huge documents

Vous aimerez peut-être aussi