Some Usage Examples of My XmlToArray Class
July 4th, 2007I am very happy that my XmlToArray Class at PHPClasses.Org got a huge popularity. This class parses XML and forms array from the hierarchical XML format. You can download it from here:
http://www.phpclasses.org/browse/package/2938.html
Recently few users requested me to provide a real-life example usage of my class. In this regard, I want to show two examples here:
BBC News Feed : http://www.rupom.com/demos/bbc/
Cricinfo Latest : http://www.rupom.com/demos/cricinfo/
I want to share my usage.php that I used for BBC News Feed. Here it is:
————————-
require(“XmlToArray.class.php”);
// Retrieving the RSS first. I used file_get_contents(), you can retrieve by any other way
$xml_data = file_get_contents(“http://newsrss.bbc.co.uk/rss/ newsonline_world_edition/front_page/rss.xml”);
//Creating Instance of the Class
$xmlObj = new XmlToArray($xml_data);
//Creating Array
$arrayData = $xmlObj->createArray();
$rssData = array();
//getting the feed items
$rssData = $arrayData[‘rss’][‘channel’][0][‘item’];
// data is available; loop through the data
foreach($rssData as $i=>$v)
{
$title = $v[‘title’];
$link = $v[‘link’];
$category = $v[‘category’];
$pubDate = $v[‘pubDate’];
$desc = $v[‘description’];
/* print data now
….
…..
…..
*/
}
————————-
Isn’t this the simplest script? Yes, I tried to make it as simple as possible. Please feel free to let me know more queries.
Best regards.
Rupom