Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 202
0 Users 4 Guests Online

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 MOD-Shadows tutorial.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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.


1
2
<?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.

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.
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.
1
2
3
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 articles and to end the PHP script.

1
2
}
?>


And for all you copy + paster's out there.
1
2
3
4
5
6
7
8
9
<?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";
}
?>
DanielXP
Author:
Views:
7197
Rating:
Posted on Friday 7th November 2008 at 09:46 PM
m4mb0
m4mb0
How do i change the charset of the text and how can i get the date from it.