I 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
What about nodes more than one? i mean, if there are siblings it gives only the last node and its descents as an array. eg.
only the 3rd stock node returns as array.
LikeLike
thank you very much
LikeLike
very interesting.
i’m adding in RSS Reader
LikeLike
i try to send via POST this:
$xmlObj = new XmlToArray( addslashes( $_POST[‘xmls’]) );
$arrayData = $xmlObj->createArray();
………………..
but nothing doit??
when i try this, i have no problem??:
$xml_data = ”
114
121
79
78
…………..
LikeLike
I just came across this really great script. Thank you. The only problem I have is the one that cabbar described above (gives you only the last node if there are siblings). I haven`t found a newer script than 1.0. Is there a solution for this?
Greetings
W
LikeLike
Yes as it converts xml into array, only the last one gets placed for the same named nodes (you know two same level indexes can’t have same index name). Please give me your example XML that you faced problem for. I will give you a solution.
Thanks.
LikeLike
hi,
this is my array( it is a 3D matrix)
i want to save it in the xml file
and regenerate the matrix from the xml file
my initial array ( 3d array)
Array
(
[I499] => Array
(
[o] => Array
(
[o] => 2
)
)
[Z098] => Array
(
[I495] => Array
(
[o] => 2
[Z950] => 2
[Z952] => 1
[Z958] => 1
[Z952jn] => 1
)
[N185] => Array
(
[Z992+1] => 1
[Z941] => 1
[I159] => 1
[M814] => 1
[D638] => 1
[Z950] => 1
)
)
[I340] => Array
(
[o] => Array
(
[I500] => 1
[I511] => 1
[N179] => 1
[N189] => 1
[K259] => 1
[K269] => 1
[R410] => 1
[R2630] => 1
[I48] => 1
)
)
[I501] => Array
(
[o] => Array
(
[I511] => 1
[N179] => 2
[R410] => 2
[R2630] => 2
[I48] => 1
[I512] => 3
[I48jn] => 1
)
)
)
Thanks for you help
LikeLike
Excellent site, keep up the good work.
LikeLike