Parsing XML/RSS feeds easily with SimpleXML
by Dotnetindex Monday, March 10, 2008
Rating:
Vote this news:
In this tutorial, we will try to parse an RSS feed using SimpleXML . SimpleXML is a new focus provided in PHP5. It's a very useful object. This powerful object converts XML data to object. SimpleXML is enabled by default.
First example:
<?php
$xml = new SimpleXMLElement('http://www.dotnetindex.com/rss.asp' ,null, true);
echo $xml->channel->item[1]->title . '<br/>'. "\n";
?>
Only two lines of code is enough to display any data from XML source. That looks awesome! Ok. let's play more at codes. Now we will parse all rss feed of Dotnetindex.com.
Second example:
<?php
$xml = new SimpleXMLElement('http://www.dotnetindex.com/rss.asp' ,null, true);
foreach ($xml->channel->item as $items)
{
echo "<a href=\"$items->link\"><b>$items->title</b></a><br/>\n";
echo "$items->description</a><br/>\n";
}
?>
Easily done. All data processed with normal property selectors and array iterators. Only we have looped them and placed a little bit of HTML codes.
Happy Coding



