Some Usage Examples of My XmlToArray Class

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/

Screenshot of XmlToArray Usage Example
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

8 thoughts on “Some Usage Examples of My XmlToArray Class

  1. cabbar July 17, 2007 / 6:07 am

    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.

    Like

  2. rss November 20, 2007 / 9:49 am

    thank you very much

    Like

  3. music January 7, 2008 / 7:35 am

    very interesting.
    i’m adding in RSS Reader

    Like

  4. fanis March 15, 2008 / 6:14 am

    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

    …………..

    Like

  5. Wolfgang March 16, 2008 / 10:20 pm

    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

    Like

  6. Rupom March 19, 2008 / 8:46 pm

    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.

    Like

    • yao October 4, 2014 / 1:31 pm

      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

      Like

  7. Bill Bartmann September 4, 2009 / 4:26 am

    Excellent site, keep up the good work.

    Like

Leave a reply to Bill Bartmann Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.