0

Possible Duplicate:
Parse XML with Namespace using SimpleXML
PHP SimpleXML Namespace Problem

<?php
header("Content-Type: text/html; charset=utf-8");
$str = <<<ETO
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:bc="http://www.brightcove.tv/link" xmlns:dcterms="http://purl.org/dc/terms/">
    <channel>
        <item>
            <title>Press Conference with Chairman of the FOMC, Ben S. Bernanke</title>
            <link>http://link.brightcove.com/services/link/bcpid720309829001/bctid1414417812001?src=mrss</link>
            <description>Chairman of the FOMC, Ben S. Bernanke, Washington, D.C.</description>
            <pubDate>Thu, 26 Jan 2012 11:57:19 -0800</pubDate>
            <media:player height="580" url="http://link.brightcove.com/services/link/bcpid720309829001/bctid1414417812001?src=mrss" width="440"/>
            <media:thumbnail height="90" url="http://brightcove.vo.llnwd.net/d20/unsecured/media/66043936001/66043936001_1414432575001_fomc-presser-thumb.jpg?pubId=66043936001" width="120"/>
            <media:thumbnail height="360" url="http://brightcove.vo.llnwd.net/d20/unsecured/media/66043936001/66043936001_1414432069001_fomc-presser-large.jpg?pubId=66043936001" width="480"/>
            <bc:duration>4016</bc:duration>
        </item>
        <item>
            <title>Press Conference with Chairman of the FOMC, Ben S. Bernanke</title>
            <link>http://link.brightcove.com/services/link/bcpid720309829001/bctid1258170578001?src=mrss</link>
            <description>Chairman of the FOMC, Ben S. Bernanke, Washington, D.C.</description>
            <pubDate>Thu, 26 Jan 2012 08:21:02 -0800</pubDate>
            <media:player height="580" url="http://link.brightcove.com/services/link/bcpid720309829001/bctid1258170578001?src=mrss" width="440"/>
            <media:thumbnail height="90" url="http://brightcove.vo.llnwd.net/d16/unsecured/media/66043936001/66043936001_1014046968001_DSC-95132.jpg?pubId=66043936001" width="120"/>
            <media:thumbnail height="360" url="http://brightcove.vo.llnwd.net/d16/unsecured/media/66043936001/66043936001_1014127979001_DSC-95132.jpg?pubId=66043936001" width="480"/>
            <bc:duration>2897</bc:duration>
        </item>
    </channel>
</rss>
ETO;
$xmlObj = simplexml_load_string($str);
foreach($xmlObj->channel->item as $item){
    echo $item->title.'<br />';
    echo $item->link.'<br />';
    echo $item->description.'<br />';
    echo $item->pubDate.'<br />';
    echo $item->xpath('media:player').'<br />';
    echo $item->xpath('media:thumbnail',0)->url.'<br />';
    echo $item->xpath('media:thumbnail',1)->url.'<br />';
    echo $item->xpath('bc:duration').'<br />';
    echo '<hr />';
}
?>

How to parse some node like media:player,media:thumbnail, bc:duration? I met some wrong, 2 string array and 2 wrong...

Array
Warning: SimpleXMLElement::xpath() expects exactly 1 parameter, 2 given in E:\www\1.php on line 37 
Warning: SimpleXMLElement::xpath() expects exactly 1 parameter, 2 given in E:\www\1.php on line 39 
Array
Community
  • 1
  • 1
fish man
  • 2,666
  • 21
  • 54
  • 94

2 Answers2

2

This gets you, what you want:

$hlp = $item->xpath('media:thumbnail[1]');
echo $hlp[0]['url'];

Maybe, there are better methods...I'll see, if I can find some.

Regarding the errormessage, ThinkingMonkey is right - you used the function incorrectly.

--

edit: This is probably a faster and more beautiful solution:

$media = $item->children('http://search.yahoo.com/mrss/');
$hlp = $media->player->attributes();
echo $hlp['url'].'<br />';
$hlp = $media->thumbnail[0]->attributes();
echo $hlp['url'].'<br />';
$hlp = $media->thumbnail[1]->attributes();
echo $hlp['url'].'<br />';
Max
  • 1,000
  • 1
  • 11
  • 25
1

Most part of your code looks fine except for the use of function xpath()

From SimpleXMLElement::xpath manual

public array SimpleXMLElement::xpath ( string $path )

It accepts only one parameter! you are passing 2 params:

echo $item->xpath('media:thumbnail',0)->url.'<br />';
echo $item->xpath('media:thumbnail',1)->url.'<br />';

You have used it correctly here!

echo $item->xpath('media:player').'<br />';

Just do this to access your media:thumbnail. Since, they will be parsed & stored as array:

echo $item->xpath('media:thumbnail[0]')->url.'<br />';
echo $item->xpath('media:thumbnail[1]')->url.'<br />';
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
  • `echo $item->xpath('media:player').'
    ';` return `array`, and how to make difference from 2 'media:thumbnail'? Thanks.
    – fish man Jan 31 '12 at 20:27