-2

i am new to php. i need to do a task that convert xml to array i search out a lot on the net and found lot of functions classes doing this for me but every function needs arguments which i don't understand how to pass it the correct arguments. i need a function which take the xml of this link

  http://feeds.nytimes.com/nyt/rss/SundayBookReview

if you press ctrl+u it will give you the source it will b xml i need that xml through php and then convert it into an array please help me out.i am stuck

2 Answers2

1

this is what i use:

<?php

function xmlobj2arr($Data)
{
   if (!isset($ret)) { $ret = array(); }
   if (is_object($Data))
      { foreach (get_object_vars($Data) as $key => $val) { $ret[$key] = xmlobj2arr($val); } return $ret; }
   elseif (is_array($Data)) {
      foreach ($Data as $key => $val) { $ret[$key] = xmlobj2arr($val); } return $ret;
   } else { return $Data; }
}

$thexml = new SimpleXMLElement( file_get_contents("http://feeds.nytimes.com/nyt/rss/SundayBookReview") );
$arr = xmlobj2arr($thexml);
echo "<pre>";
print_r($arr);
echo "</pre>";

?>
  • where is the class for $thexml = new SimpleXMLElement(... ???? – user1133861 May 28 '12 at 13:10
  • the object is the $thexml; the ???? shows the feed, which can be a valid XML data (string) or a file –  May 28 '12 at 13:16
  • no i mean i can see the object in the code but i cannot see the class for which you made an object i need that class as wel – user1133861 May 28 '12 at 13:18
  • The "SimpleXMLElement" is an extension, enabled by default in PHP5; there's no download. http://www.php.net/manual/en/simplexml.requirements.php –  May 28 '12 at 13:19
0

Use simplepie

Its very easy and simple. Download it and check the demo.

Check demo code here

Venu
  • 7,243
  • 4
  • 39
  • 54