ivytony Posted January 29, 2009 Share Posted January 29, 2009 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? Link to comment https://forums.phpfreaks.com/topic/143036-how-to-get-the-element-in-the-tag-in-an-xml-file/ Share on other sites More sharing options...
DarkSuperHero Posted January 29, 2009 Share Posted January 29, 2009 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... Link to comment https://forums.phpfreaks.com/topic/143036-how-to-get-the-element-in-the-tag-in-an-xml-file/#findComment-750102 Share on other sites More sharing options...
ivytony Posted January 29, 2009 Author Share Posted January 29, 2009 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 Link to comment https://forums.phpfreaks.com/topic/143036-how-to-get-the-element-in-the-tag-in-an-xml-file/#findComment-750136 Share on other sites More sharing options...
gaza165 Posted January 29, 2009 Share Posted January 29, 2009 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="); } ?> Link to comment https://forums.phpfreaks.com/topic/143036-how-to-get-the-element-in-the-tag-in-an-xml-file/#findComment-750167 Share on other sites More sharing options...
phparray Posted January 30, 2009 Share Posted January 30, 2009 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. Link to comment https://forums.phpfreaks.com/topic/143036-how-to-get-the-element-in-the-tag-in-an-xml-file/#findComment-750317 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.