解析XML与PHP的SimpleXML

问题描述:

可能重复:
Parse XML with Namespace using SimpleXML
PHP SimpleXML Namespace Problem解析XML与PHP的SimpleXML

<?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 />'; 
} 
?> 

如何分析像media:playermedia:thumbnailbc:duration一些节点?我遇到了一些错误,2字符串数组和2个错......

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 
+1

或者http://*.com/questions/2098170/php-namespace-simplexml-problems – Wrikken 2012-01-31 20:13:00

+1

或http://*.com/questions/5899747/php-simplexml-issue – Wrikken 2012-01-31 20:14:28

+1

或http://*.com/questions/2991832/php-accessing-namespaced-xml-with-simplexml – Wrikken 2012-01-31 20:14:49

这让你,你想要什么:

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

也许有更好的方法......我去看看,如果我能找一些。

关于errormessage,ThinkingMonkey是正确的 - 您错误地使用了该功能。

-

编辑:这可能是一个更快,更漂亮的解决方案:

$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 />'; 

大部分代码的一部分,看起来不错,但用的功能xpath()

SimpleXMLElement::xpath manual

public array SimpleXMLElement::xpath (string $path) 

它接受只有一个参数!您正在传递2个参数:

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

您在这里正确使用它!

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

只是这样做来访问您的media:thumbnail。因为,他们将被解析&存储阵列:

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

'echo $ item-> xpath('media:player')。'
';'返回'数组',以及如何区别2'媒体:缩略图'?谢谢。 – 2012-01-31 20:27:59