1

I have a rss feed url which generates an xml as follows:

 <?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
 xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0"><channel>
 <title>flow-Media Catalog</title>
 <link>http://catalog.flownetworks.com/catalogs/1/videos.mrss</link>
 <description>Video Catalog</description>
 <image>
    <url>http://images.flow-media.com/flow_media_current.png</url>
    <title>Get to know flow-Media</title>
    <link>http://www.flow-media.com</link>
 </image>
 <generator>flow-Media</generator>
 <item>
   <title>..</title>
   <link>..</link>
   <description>..</description>
   <pubDate>Wed, 01 May 2013 07:01:08 GMT</pubDate>
   <guid isPermaLink="false">9809880</guid>
   <flow:short_description/>
   <flow:video_product_id>52985890</flow:video_product_id>
   <flow:availability end="" start="2013-05-01T06:44:41Z"/>
   <flow:show/>
   <media:group>
   <media:category>US</media:category>
   <media:thumbnail url="http://images.flow-media.com/ap/2013/05/01/09d462107646ab09def454a1a923a423edd6d2d9_preview.jpg" height="360" width="640"/>
   <media:thumbnail url="http://images.flow-media.com/ap/2013/05/01/09d462107646ab09def454a1a923a423edd6d2d9_thumbnail.jpg" height="90" width="160"/>
   <media:content duration="101" medium="video" isDefault="true" url="http://player.flownetworks.com/swf/cube.swf?a=V5299690&amp;m=9&amp;w=420&amp;h=375" type="application/x-shockwave-flash" expression="full" height="375" lang="en-us" width="420">
    </media:content>
    </media:group>
   </item>
   </channel>

I want to save image and category value in database.Here is my php code:

$apiURL="http://catalog.flownetworks.com/catalogs/01/videos/search.mrss?api_key=%206784cb3bed8f80c55054ac0de996f8e9f0bf8763";
$videoId="&video_product_id=".$videoID."";
$combinevURL=$apiURL.$videoId;
$mediafile = simplexml_load_file($combinevURL); 

Problem: the problem is "simplexml_load_file" do not generate category name and media thumbnail image value of the xml.I want to get value from this xml.Please help.

CodePlayer
  • 171
  • 5
  • 19
  • check out http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names parsing XML with a colons in tag names – Waygood May 01 '13 at 12:31
  • @Waygood $mediafile->children('flow', true)->media->category; still it do not return anything – CodePlayer May 01 '13 at 12:42
  • possibly: $mediafile->item->children('media', true)->group->category – Waygood May 01 '13 at 12:45
  • 1
    unfortunately,it didnt.."Node no longer exists" – CodePlayer May 01 '13 at 12:49
  • i updated to $mediafile->item->children('media', true)->group->category – Waygood May 01 '13 at 12:50
  • neither of yours nor $mediafile->children('flow', true)->media->category; $mediafile->children('flow', true)->media->thumbnail; both didn't – CodePlayer May 01 '13 at 12:53
  • missing GROUP in those – Waygood May 01 '13 at 13:03
  • your XML is not valid, check http://validator.w3.org/check – michi May 01 '13 at 16:40
  • There are plenty of existing questions which cover this. One (common) mistake you're making is to think that SimpleXML is "not generating" nodes because they don't show in `print_r`, but SimpleXML isn't like that - it gives you data when you ask for it rather than creating one big object that contains everything ready. – IMSoP May 01 '13 at 16:40

1 Answers1

4

To successfully obtain these media elements you first of all need to find the parent element. How to access those non-namespaced elements is outlined in depth in the basic Simplexml usage examples, I spare you this highly redundant code here.

So after obtaining the parent into a variable - let's call it $item this time - it works as outlined in the already hinted duplicated Q&A material, just here specific to your example XML:

$media = $item->children('media', true);
                            |
                          __|__
 <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
                    xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0">

By using the namespace-prefix, it corresponds to the highlighted prefix part. This requires to use true as second parameter.

You can also use the alternative, the namespace-URI, so you can spare the second parameter (defaults to FALSE):

$media = $item->children('http://search.yahoo.com/mrss/');
                                        |
                                 _______|_____________________
 <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
                    xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0">

Regardless which variant you prefer, telling simplexml exactly that you are concerned about children in the media XML-namespace enables you to access the various parts from within that media group as you know it:

$group = $media->group;
echo $group->category, "\n"; # US

I hope this is helpful and explicitly shows you how it works for the namespaced elements. You need to use the SimpleXMLElement::children() method and specify which namespace you mean of getting the children elements from.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • @CodePlayer: (only if you like, I ask to make this better) Can you share what your problem was to obtain this information? Did you know for example that XML has namespaces? Or did those elements just looked like normal elements to you and you didn't notice that those might behave differently? – hakre May 07 '13 at 11:07