Parsing XML Feeds in PHP with simplexml
Tuesday, December 30, 2008 13:56Parsing XML feeds is always something I’ve wanted to to do with PHP. But I’ve only bothered to learn how to do it. Now, there are different ways to do this, many of which uses different packages/plugins which need to be installed on the server. I am going to use simplexml, since it’s built into the PHP core. I’ve created a really simple XML file, which I am going to use for this.
< ?xml version="1.0" encoding="UTF-8"?>Satya 24/11/2009 we have 3 posts in this silly little XML example file! Satya 23/11/2009 So, this is the second post! Satya 22/11/2009 This is the first post, in this simple little XML example.
You can see, there are 3 post tags under the global recentposts tag. Each of them contains tags which contain information on the title, author, date, time and content of the post. A simple structure. We begin writing the PHP to parse the information in this XML file and display it in regular HTML.
// URL of the XML feed.
$feed = 'example.xml';
// How many items do we want to display?
$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
die('The XML file could not be found!');
}
We need to tell the script where the XML is located. If it’s in the same directory as our script, a simple filename will do. If it’s hosted externally, the full URL will be needed (parsing external XML files will not work if allow_url_fopen is not enabled on your server).
Secondly, the maximum number of items we want to display from our XML file? In this example, I’ve gone for 3, which is all my XML file contains.
Now, we should check to see if the XML file actually exists, and if not, stop the scripts.
// First, open the XML file. $xml = simplexml_load_file($feed); // Set the counter for counting how many items we've displayed. $counter = 0;
We need to use the simplexml_load_file function, and place it into a variable, in this example $xml and we initialise a new variable and set it to 0.
// Start the loop to display each item.
foreach($xml->post as $post) {
echo '
' . $post->title . '
Written by ' . $post->author . ' on ' . $post->date . ' at ' . $post->time . '
' . $post->content . '
';
// Increase the counter by one.
$counter++;
// Check to display all the items we want to.
if($counter == $display) {
// Yes. End the loop.
break;
}
// No. Continue.
}
foreach($xml->post as $post)
This initialises a foreach loop. Everything inside this loop while be executed for every post item in the XML file. Which this line is doing, is taking all the information found under post in the feed, and assigning it to the $post variable. This information will then be able to accessed using $post->title, $post->author, etc. If there is more than one item in the feed (which in this case, there is: 3 posts), the information will be put into arrays. for example, displaying only the title of the most recent post would be done using $post->title[0]. However, in this example, we neednot concern ourselves with arrays, since the foreach loop will automatically display it all for us.
echo '' . $post->title . '
Written by ' . $post->author . ' on ' . $post->date . ' at ' . $post->time . ' ' . $post->content . ' ';
We are simply displaying the title of the post in a h1 tag, displaying the author, date and time of the post as italics, and then displaying the main content of the post.
// Increase the counter by one. $counter++;
After every iteration of the loop, the contents of the $counter variable we initialised earlier will be incremented by one. This is so the script know how many posts we have so far displayed in total.
// Check to display all the items we want to.
if($counter == $display) {
// Yes. End the loop.
break;
}
// No. Continue.
}
The script will check if $counter is equal to $display (have we displayed the maximum amount of posts we want to display?), and if this is indeed the case, break out of the foreach loop, thus ending the script. If not, nothing happens, and the loop begins once again.
So, putting all that together, this final script will look something like this:
< ?php
// URL of the XML feed.
$feed = 'example.xml';
// How many items do we want to display?
$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
die('The XML file could not be found!');
}
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
echo '
' . $post->title . '
Written by ' . $post->author . ' on ' . $post->date . ' at ' . $post->time . '
' . $post->content . '
';
// Increase the counter by one.
$counter++;
// Check to display all the items we want to.
if($counter == $display) {
// Yes. End the loop.
break;
}
// No. Continue.
}
?>
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.


Parsing XML Feeds in PHP with simplexml « Sathyendra’s Blog says:
December 30th, 2008 at 3:02 pm
[...] Parsing XML Feeds in PHP with simplexml By sathyendra Parsing XML Feeds in PHP with simplexml [...]
watch satellite tv on pc says:
January 8th, 2009 at 6:05 am
programming packages…
Can point me to other similar posts? Really appreciate it. Thanks….
Bryan says:
January 8th, 2009 at 10:28 pm
I really like this tutorial and thought it was really helpful. I have one question that you might be able to help with. What do I do if one of my xml tags looks like this ?
How do I get the information out of that tag? I tried for example, using your example, $post->xc:country. But, this doesn’t seem to work.
PHP_Starter says:
January 24th, 2009 at 12:01 pm
Please give me the compleat code, so that i can help you
Parsing XML Feeds in PHP with simplexml | CLD Tutorials says:
January 27th, 2009 at 7:24 am
[...] View tutorial Tutorial Stats : 2 views Rate Tutorial : (No Ratings Yet) Loading … DiggStumbleDel.icio.usFloatBumpMixxRedditzaBoxother [...]
Inetminds.com - Illuminations says:
January 27th, 2009 at 8:53 am
[...] Inetminds.com. Tags: php, xml [...]