Parsing a RSS feed


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 ShadowMage's tutorial.

XML Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <rss version="2.0">
  3. <channel>
  4. <item>
  5. <title>Pizza Hut sued for discrimination</title>
  6. <link>https://news.bbc.co.uk/1/hi/england/lancashire/7238636.stm</link>
  7. <description>Three gay men from Wales are suing Pizza Hut for sexual discrimination after they were refused service at a restaurant in Blackpool.</description>
  8. </item>
  9. <item>
  10. <title>Microsoft Wants To Buy Yahoo</title>
  11. <link>https://news.bbc.co.uk/1/hi/business/7222114.stm</link>
  12. <description>Microsoft has offered to buy the search engine company Yahoo for $44.6bn (�22.4bn) in cash and shares.</description>
  13. </item>
  14. <item>
  15. <title>Google</title>
  16. <link>https://google.com</link>
  17. <description>Google is a good search engine. If you search a errors you get you can find the problem with it!</description>
  18. </item>
  19. </channel>
  20. </rss>


Ok now lets start with parsing the XML with PHP.


PHP Code
  1. <?php
  2. $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 Code
  1. $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 Code
  1. 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 Code
  1. echo "<a href="" . $feeditem->getElementsByTagName('link')->item(0)->nodeValue . ""><b>";
  2. echo $feeditem->getElementsByTagName('title')->item(0)->nodeValue . "</b></a>n";
  3. 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>https://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 articles and to end the PHP script.

PHP Code
  1. }
  2. ?>


And for all you copy + paster's out there.
PHP Code
  1. <?php
  2. $xmldoc = new DOMDocument();
  3. $xmldoc->load('xmldocument.xml');
  4. foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
  5. echo "<a href="" . $feeditem->getElementsByTagName('link')->item(0)->nodeValue . ""><b>";
  6. echo $feeditem->getElementsByTagName('title')->item(0)->nodeValue . "</b></a>n";
  7. echo $feeditem->getElementsByTagName('description')->item(0)->nodeValue . "n";
  8. }
  9. ?>
DanielXP's Avatar
Author:
Views:
7,532
Rating:
Posted on Friday 7th November 2008 at 09:46 PM
m4mb0
m4mb0's Avatar
How do i change the charset of the text and how can i get the date from it.