Parsing a RSS feed - RMB Scripting
You appear not have have an account with us, Click HERE to register an account...
RMB Scripting
Navigation
  Home
  Contact
  FAQ
  Forum
  Topsites
  Members
  Affiliates
  Arcade <-- New!
  IoxHost
Downloads
  Templates
  Scripts
  Fonts
  Image Sets
  Software
  Other
 
Tutorials
  .htaccess
  Photoshop
  C/C++
  CSS
  Macromedia Flash
  MySQL
  PHP
  PHP - User System
  HTML
  Javascript
  Macintosh
  Macromedia Fireworks
  Visual Basic
  Visual C#
  Windows
  Python
  Other
 
Latest Tutorials
All PHP / Apache Funct...
Block User\'s Direct A...
Generate a salt
Image Tag Tracking
Cpanel/webmail Login
Latest Comments
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Steal msn picture
Simple and minimalisti...
Stats
Total Members: 562 [216]
Total Tutorials: 269
Newsest User: Stf
Todays Unique Hits: 50
Users Online: 0
 
7 Guests
Poll
Should we change our name?
Hmmmm, Yes!!
Maybe
Depends on what to
NO!
Main Content
Tutorial: Parsing a RSS feed By DanielXP
Ok lets start of with the RSS feed, below is an example of what one would look like.

If you want to create a RSS feed using PHP then click here to view MOD-Shadows tutorial.

   
CODE:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<item>
<title>Pizza Hut sued for discrimination</title>
<link>http://news.bbc.co.uk/1/hi/england/lancashire/7238636.stm</link>
<description>Three gay men from Wales are suing Pizza Hut for sexual discrimination after they were refused service at a restaurant in Blackpool.</description>
</item>
<item>
<title>Microsoft Wants To Buy Yahoo</title>
<link>http://news.bbc.co.uk/1/hi/business/7222114.stm</link>
<description>Microsoft has offered to buy the search engine company Yahoo for $44.6bn (£22.4bn) in cash and shares.</description>
</item>
<item>
<title>Google</title>
<link>http://google.com</link>
<description>Google is a good search engine. If you search a errors you get you can find the problem with it!</description>
</item>
</channel>
</rss>


Ok now lets start with parsing the XML with PHP.


   
PHP:
<?php
$xmldoc 
= new DOMDocument();


OK here we created a new DOMDocument object using the DOM class.
For more information on DOMDocument click here.


Now lets move onto loading the XML document into PHP so we can parse it.

   
PHP:
$xmldoc->load('xmldocument.xml');


This will load the XML document into the variable $xmldoc.
For more information on DOMDocument (load) click here.


Now its time to display each of the items of the RSS document onto our page.
   
PHP:
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {


This will go through the XML document looking for the tags named 'item' aka each article of our RSS feed (The content between the tags <item>...</item>.


Now we have each article from the feed lets display it to the user.
   
PHP:
echo "<a href=\"" . $feeditem->getElementsByTagName('link')->item(0)->nodeValue . "\"><b>";
echo $feeditem->getElementsByTagName('title')->item(0)->nodeValue . "</b></a>\n";
echo $feeditem->getElementsByTagName('description')->item(0)->nodeValue . "\n";


This code echos each article out by using the same method as splitting up each article for each part of the article.
This time we are splitting each article into 3 different values what are,

title -> Title of the story e.g. <title>Pizza Hut sued for discrimination</title>
link -> The link of the story e.g. <link>http://news.bbc.co.uk/1/hi/england/lancashire/7238636.stm</link>
description -> Description of the story e.g. (na I think you get it now.)

So basic we have made a tree.

item (1)
     title
     link
     description
item (2)
     title
     link
     description
item (3)
     title
     link
     description

Now all that is left is to close the foreach of the articals and to end the PHP script.

   
PHP:
}
?>


And for all you copy + pasters out there.
   
PHP:
<?php
$xmldoc 
= new DOMDocument();
$xmldoc->load('xmldocument.xml');
foreach (
$xmldoc->getElementsByTagName('item') as $feeditem) {
echo 
"<a href=\"" $feeditem->getElementsByTagName('link')->item(0)->nodeValue "\"><b>";
echo 
$feeditem->getElementsByTagName('title')->item(0)->nodeValue "</b></a>\n";
echo 
$feeditem->getElementsByTagName('description')->item(0)->nodeValue "\n";
}
?>

Difficulty: Moderate
Views: 2283
Rating:
Comments
Posted on Friday 7th November 2008 at 09:46 PM
m4mb0
How do i change the charset of the text and how can i get the date from it.
Notice:
Remember to post long codes on our pastbin! - (http://rmb.pastebin.com/)
Add Comment
You must be logged in to post a comment.
Good Morning Guest
Username: 
Password: 
Remember Username
Links
RMB Arcade
$2.50 Resellers
Free Domains
Free Games
Affiliates