Location: PHPKode > scripts > DOMIT XML parser > domit-xml-parser/rss_example.html
<html>
<head>
<title>Parsing an RSS 0.9x Feed with DOMIT!</title>
</head>
<body>

<h2>Parsing an RSS 0.9x Feed with DOMIT!</h2>

<h4>Introduction to RSS Feeds</h4>

<p>A Really Simple Syndication (RSS) feed is an XML-based format for the 
syndication of lists of hyperlinks and related metadata. <p>

<p>A typical example is the distribution of news:</p>

<p>
<ul>
	<li><p>RSS XML data is posted at a publicly accessible url. 
	The XML contains up-to-date headlines, a brief description of each headline, 
	and a link to the complete news article.</p></li>
	<li><p>A web application will, when requested, grab the data at the specified url, 
	parse the XML, and display the news links as HTML.</p></li>
	<li><p>The user who has triggered the request will click on one of the news links 
	and be taken to the location of the complete news article.</p></li>
</ul>	
</p>

<p>Writing a web application that handles RSS of course requires an XML parser. 
In the following example, we will use the DOMIT! XML parser to process a simple RSS 0.9x news feed.</p>

<h4>Building a Simple RSS 0.9x Feed Processor</h4>

<p>The site <a href="http://www.feedroom.com/rssmain.jsp" target="_child">The FeedRoom</a> 
maintains a list of RSS feeds from which I randomly chose one:</p>

<p><a href="http://www.feedroom.com/rssout/att_rss_1ebaad7be9f5b75e7783f8b495e59bd0f58380b9.xml" target="_child">
Top Stories from The AT&amp;T Worldnet FeedRoom</a> (click to view the RSS XML source) is a typical RSS feed that serves up 
a periodically updated list of 10 leading news headlines, a short description of each, and 
a link to video clips of the stories.</p>

<p>Parsing this XML string with PHP and DOMIT! is a relatively simple task. First the XML text is grabbed from the url 
using the PHP function file_get_contents($myUrl):</p>

<p>
<pre>
<type>//get rss xml from feed "Top Stories from The AT&ampT Worldnet FeedRoom"
$myUrl = "http://www.feedroom.com/rssout/att_rss_1ebaad7be9f5b75e7783f8b495e59bd0f58380b9.xml";
$rss = file_get_contents($myUrl);</type>
</pre>
</p>

<p>An instance of  <b>DOMIT_Document</b> is then instantiated:</p>

<p>
<pre>
<type>//create new DOMIT_Document
require_once("xml_domit_parser.php");
$rssDoc = new DOMIT_Document();</type>
</pre>
</p>

<p>The document is populated by passing the XML string to the <b>parseXML</b> method:</p>

<p>
<pre>
<type>//parse RSS XML
$rssDoc->parseXML($rss, true);</type>
</pre>
</p>

<p>The DOMIT_Document can now be traversed using standard DOM methods and some 
appropriate HTML displayed to the end user. Before we can do this, we need to know how the
XML in an RSS document is structured.</p>

<p>A typical RSS 0.9x XML structure is comprised of "channel" and "item" nodes.</p> 

<p>
<ul>
	<li><p>Channel nodes are, as the name imples, groupings of related links.</p></li>
	<li><p>Each channel contains a set of nodes that describe itself.</p></li>
	<li><p>Each channel also contains a list of item nodes, which contain the links and links descriptions.</p></li>
</ul>
</p>

<p>A channel node generally looks something like this:</p>

<p>
<pre>
<type>&lt;channel&gt;
  &lt;title&gt;Top Stories from The AT&amp;T Worldnet FeedRoom&lt;/title&gt;
  &lt;language&gt;en-us&lt;/language&gt;
  &lt;link&gt;http://worldnet.feedroom.com/?rf=rss&amp;fr_chl=1ebaad7be9f5b75e7783f8b495e59bd0f58380b9&lt;/link&gt;
  &lt;description&gt;The AT&amp;T Worldnet FeedRoom: Top Stories&lt;/description&gt;
  &lt;pubDate&gt;11/26/2003 17:10:58 EST&lt;/pubDate&gt;

    &lt;item&gt;
      &lt;title&gt;Busy Holiday Travel Day&lt;/title&gt;
      &lt;description&gt;Nov. 26 - Officials say more Americans will be hitting the roads and taking 
      to the sky this holiday weekend than last year, leading to heavy traffic and crowded airports.&lt;/description&gt;
      &lt;link&gt;http://worldnet.feedroom.com/?rf=rss&amp;fr_story=FEEDROOM59209&lt;/link&gt;
    &lt;/item&gt;

    &lt;item&gt;
      &lt;title&gt;Sniper Car Video Released&lt;/title&gt;
      &lt;description&gt;Nov. 26 - Officials Tuesday allowed the media to photograph the car that 
      authorities say served as the sniper platform during the 2002 killing spree.&lt;/description&gt;
      &lt;link&gt;http://worldnet.feedroom.com/?rf=rss&amp;fr_story=FEEDROOM59243&lt;/link&gt;
    &lt;/item&gt;
&lt;/channel&gt;
</pre>
</p>

<p>Since we now have the RSS feed parsed into a <b>DOMIT_Document</b>, and we know beforehand 
its structure, we can devise an algorithm to  parse through it and display the feed to the user as HTML.</p>

<p>First, we ascertain the number of channels in the RSS feed, echo out some html header info, 
and set up a loop to iterate through each channel:</p>

<p>
<pre>
<type>$numChannels = count($rssDoc-&gt;documentElement-&gt;childNodes);

echo ("&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Sample RSS 0.9x Feed Display&lt;/title&gt;\n&lt;/head&gt;\n\n&lt;body&gt;\n");

for ($i = 0; $i &lt; $numChannels; $i++) {</type>
</pre>
</p>

<p>We then get a reference to the current channel and its meta information, then echo it to the end user:</p>

<p>
<pre>
<type>	$currentChannel =& $rssDoc-&gt;documentElement-&gt;childNodes[$i];
	$channelTitle = $currentChannel-&gt;childNodes[0]-&gt;firstChild-&gt;nodeValue;
	$channelDesc = $currentChannel-&gt;childNodes[3]-&gt;firstChild-&gt;nodeValue;
	$channelPubDate = $currentChannel-&gt;childNodes[4]-&gt;firstChild-&gt;nodeValue;
	
	echo ("&lt;h2&gt;$channelTitle&lt;/h2&gt;\n&lt;h4&gt;($channelDesc - $channelPubDate)&lt;/h4&gt;\n");</type>
</pre>
</p>
		
<p>We can now iterate through each of the channel items and echo the data to the end user:</p>		

<p>
<pre>
<type>	$numChannelNodes = count($currentChannel-&gt;childNodes);
		
	//parse out items data
	for ($j = 5; $j &lt; $numChannelNodes; $j++) {
		$currentItem = $currentChannel-&gt;childNodes[$j];
			
		$itemTitle = $currentItem-&gt;childNodes[0]-&gt;firstChild-&gt;nodeValue;
		$itemDesc = $currentItem-&gt;childNodes[1]-&gt;firstChild-&gt;nodeValue;
		$itemLink = $currentItem-&gt;childNodes[2]-&gt;firstChild-&gt;nodeValue;
			
		echo ("&lt;p&gt;&lt;a href=\"$itemLink\" target=\"_child\"&gt;$itemTitle&lt;/a&gt; - $itemDesc&lt;/p&gt;\n\n");
	}
}</type>
</pre>
</p>

<p>After completing the html footer the user is presented with the feed data in a usable format:</p>

<p>
<pre>
<type>echo ("&lt;/body&gt;\n&lt;/html&gt;");</type>
</pre>
</p>

<p><a href="example_domit_rss.zip" target="_child">Click here to download the source.</a></p>

<p><a href="http://www.engageinteractive.com/domit/" target="_child">Click here to view the main DOMIT! tutorial</a></p>

</body>
</html>
Return current item: DOMIT XML parser