tags:

views:

42

answers:

1

I'm struggling to parse an XML file in PHP:

Here's the XML

<rss xmlns:ac="http://palm.com/app.catalog.rss.extensions" version="2.0">  
       <channel>
   <title>Device App Updates for US</title>
   <link>http://www.palm.com&lt;/link&gt;
   <description>Updates</description>
   <language>en-US</language>
   <pubDate>Mon, 04 Jan 2010 18:23:51 -0800</pubDate> 
  <lastBuildDate>Mon, 04 Jan 2010 18:23:51 -0800</lastBuildDate>
   <ac:distributionChannel>Device</ac:distributionChannel>
   <ac:countryCode>US</ac:countryCode>
   <item>
    <title><![CDATA[My App]]></title>
    <link>http://developer.palm.com/appredirect/?packageid=com.palm.myapp&lt;/link&gt;
    <description><![CDATA[My fun app.]]></description>
    <pubDate>2009-12-21 21:00:58</pubDate>
    <guid>334.232</guid>    
    <ac:total_downloads>1234</ac:total_downloads>
    <ac:total_comments>12</ac:total_comments>    
    <ac:country>US</ac:country>

   </item>
  </channel>
 </rss>

My Problem is; when I use:

$strURL = "http://developer.palm.com/rss/D/appcatalog.update.rss.xml";

 $ch = curl_init($strURL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 $data = curl_exec($ch);
 curl_close($ch);
 $doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
        print_r($doc);

I'm not able to display the values? The one I'm most interested in is <ac:total_downloads>1000</ac> but I don't seem to be able to parse it.

What am I doing wrong?

Many thanks

+1  A: 

You don't need to use curl to retrieve that file, SimpleXML can fetch external resources.

Use children() to access namespaced nodes. Here's how to do it:

$rss = simplexml_load_file($strURL);
$ns  = 'http://palm.com/app.catalog.rss.extensions';

foreach ($rss->channel->item as $item)
{
    echo 'Title: ', $item->title, "\n";
    echo 'Downloads: ', $item->children($ns)->total_downloads, "\n\n";
}
Josh Davis