Jump to content

How to get the element in the <media> tag in an XML file?


ivytony

Recommended Posts

Here's the XML file generated from MetaCafe API:

 

http://www.metacafe.com/api/item/2348336

 

I would like to parse the XML file to get the 'duration="49"' part from the <media:content tag. However, the the following code doesn't work

 

<?php
$feedUrl = 'http://www.metacafe.com/api/item/2348336';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
foreach ($xml->channel->item as $item)
{  
$content = $item->media->content;
}
echo $content;?>

 

Could someone here give me a little help again pls?

i couldnt find the text you were after but this might help in your search....

<?php 
$request_url = "http://www.metacafe.com/api/item/2348336";
$xml = simplexml_load_file($request_url) or die("feed not loading");
//var_dump($xml);  //outputs all retrieved data
echo $xml->channel->title;   //$xml is your handle, channel, and title are just nodes in your xml document....change to suit your needs...

 

 

Thanks! I was already able to get the title from the XML file, however, I am interested in fetching the value of duration in the below tag (in bold).

 

<media:content url="http://www.metacafe.com/fplayer/2348336/cute_smart_girl.swf" type="application/x-shockwave-flash" medium="video" height="240" width="320" duration="49" />

 

the tag <media:content> is still located within the <item> and </item> tags.

 

How do I get the value of duration? thanks

Why dont you search the source of the page to bring back the duration??

 

<?php 
$lines = file('http://www.metacafe.com/api/item/2348336');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    
echo strstr($line, "duration=");
}


?>

As long as the structure of the feed does not change this should work

 

<?php
$feedUrl = 'http://www.metacafe.com/api/item/2348336';
$rawFeed = file_get_contents($feedUrl);

$handle = xml_parser_create();
xml_parse_into_struct($handle, $rawFeed, $vals, $index);
xml_parser_free($handle);

echo 'Duration = '. $vals[44]['attributes']['DURATION'];

?>

 

If you are worried the structure might change you can loop through the vals array until you find the key 'DURATION' then display its value.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.