Parsing XML with PHP5 and SimpleXML

Since the introduction of PHP5, a range of XML handling functions have been added to PHP core. Why is this important I hear you ask? Well it makes life easier if you work with XML!

Below is a small script that uses SimpleXML loads and parses an RSS feed (which is XML) from this site. The feed is generated automatically by WordPress. It then displays some of the feed meta data and a list of posts contained in the feed.

    //use simplexml to load the feed
	$feed = simplexml_load_file('');
	//display the feed meta data
	echo 'Title: '.$feed->channel->title.' - '.htmlspecialchars($feed->channel->description).'<br/>'; //htmlspecialchars is used here because I happen to have <ul> used in the description of my feed.
	echo 'Site Link: <a href="'.$feed->channel->link.'" target="_new">'.$feed->channel->link.'</a><br/>';
    //search for all children of channel with a prefix of atom (of which there is one in this feed).
	$atom = $feed->channel->children("atom", true)->link->attributes(); 
	echo 'Feed Link: <a href="'.$atom["href"].'" target="_new">'.$atom["href"].'</a><br/>';
    foreach ($feed->channel->item as $entry):
		echo 'Post title: <a href="'.$entry->link.'" target="_new">'.$entry->title.'</a><br/>';
		$time = strtotime($entry->pubDate);
		$newformat = date('d-m-Y H:i',$time);
		echo 'Published: '.$newformat;


Full SimpleXML documentation can be found here.

And you can see the code in action here.