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.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<item>
<title>Pizza Hut sued for discrimination</title>
<link>https://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>https://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>https://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
$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.
$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.
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.
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>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.
And for all you copy + paster's out there.
<?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";
}
?>