-1

I have a rss xml data with contains below format..

<rss version="2.0"
  xmlns:ht="http://www.yahoo.com/gold"
  <channel>
    <item>
      <title>Title 1</title>
      <description>description 1</description>
      <ht:price>$22</ht:price>
   </item>
 </channel>

so please provide the the how to capture the child object , I have tried

 $url = "http://www.yahoo.com/gold/rss/xml";
 $xml = simplexml_load_file($url);
 for ( $i = 0; $i < 10; $i++ ){
      $title = $xml->channel->item[$i]->title;
      $description = $xml->channel->item[$i]->description;
      $goldprice =  $xml->channel->item[$i]->children('http://www.yahoo.com/gold'); 
      $itemprice = $goldprice->price;

echo "</br>" ;
echo $title;
echo "</br>" ;
echo $description;
echo "</br>" ;
echo $itemprice;
echo "</br>" ;

But I am getting error Fatal error: Call to a member function children() on a non-object in...

please provide the solution how to resolve this?

Thanks,

subbun
  • 11
  • 6

1 Answers1

1

What you have shown is obviously not the complete XML you're working with, but here's a contrived example with SimpleXML:

$xml = '<root><title>Title 1</title><description>Description 1</description><price>$22</price></root>';
//      ^^^^^^                                                                                ^^^^^^^
//      added to make this example work

$doc = simplexml_load_string($xml);
echo $doc->title; // Title 1
echo $doc->description; // Description 1
echo $doc->price; // $22

There are better tools than SimpleXML, such as DOMDocument; I would suggest investigating that instead if you have time.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309