如何解析XML的媒体:PHP内容?

我找到了一个关于如何完成大部分工作的精彩教程:

https://www.developphp.com/video/PHP/simpleXML-Tutorial-Learn-to-Parse-XML-Files-and-RSS-Feeds

但我无法理解如何提取媒体:来自Feed内容图片.我已经阅读了尽可能多的信息,但我仍然卡住了.

即:How to get media:content with SimpleXML
这建议使用:

foreach ($xml->channel->item as $news){
    $ns_media = $news->children('http://search.yahoo.com/mRSS/');
    echo $ns_media->content; // displays "<media:content>"}

但我无法让它发挥作用.

这是我正在尝试解析的脚本和提要:

<?PHP
$html = "";
$url = "http://RSSFeeds.webmd.com/RSS/RSS.aspx?RSSSource=RSS_PUBLIC";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
    $title = $xml->channel->item[$i]->title;
    $link = $xml->channel->item[$i]->link;
    $description = $xml->channel->item[$i]->description;
    $pubDate = $xml->channel->item[$i]->pubDate;

    $html .= "<a href='$link'><h3>$title</h3></a>";
    $html .= "$description";
    $html .= "<br />$pubDate<hr />";
}
echo $html;
?>

我不知道将此代码添加到脚本中以使其工作.老实说,我浏览了几个小时,但找不到可以解析媒体的工作脚本:内容.

有人可以帮忙吗?

========================

更新:

Thanx to fusion3k,我得到了最终的代码

<?PHP
$html = "";
$url = "http://RSSFeeds.webmd.com/RSS/RSS.aspx?RSSSource=RSS_PUBLIC";
$xml = simplexml_load_file($url);
for($i = 0; $i < 5; $i++){

    $image = $xml->channel->item[$i]->children('media',True)->content->attributes();
    $title = $xml->channel->item[$i]->title;
    $link = $xml->channel->item[$i]->link;
    $description = $xml->channel->item[$i]->description;
    $pubDate = $xml->channel->item[$i]->pubDate;

    $html .= "<img src='$image' alt='$title'>";
    $html .= "<a href='$link'><h3>$title</h3></a>";
    $html .= "$description";
    $html .= "<br />$pubDate<hr />";
}
echo $html;
?>

基本上我只需要这个简单的线:

$image = $xml->channel->item[$i]->children('media',True)->content->attributes();

不可否认,非技术人员在阅读了数十篇帖子和文章后,在网上找到这些信息是如此困难.好吧,希望这对我这样的其他人有用:)

获取“url”属性,请使用 – > attribute()语法:
$ns_media = $news->children('http://search.yahoo.com/mRSS/');

/* Echoes 'url' attribute: */
echo $ns_media->content->attributes()['url'];
// in PHP < 5.5: $attr = $ns_media->content->attributes(); echo $attr['url'];

/* Catches 'url' attribute: */
$url = $ns_media->content->attributes()['url']->__toString();
// in PHP < 5.5: $attr = $ns_media->content->attributes(); $url = $attr['url']->__toString();

命名空间说明:

– > children()参数不是XML的URL,而是Namespace URI.

XML名称空间用于在XML文档中提供唯一命名的元素和属性

06001

因此,在您的情况下,< media:content>是Namespace“media”的“content”元素. Namespaced元素必须具有关联的Namespace URI,作为父节点的属性或 – 最常见的 – 根元素的属性:此属性的格式为xmlns:yyy =“NamespaceURI”(在您的情况下为xmlns:media =“http:/ /search.yahoo.com/mRSS/“作为根节点的属性< RSS>).

最终,上面的$news-> children(‘http://search.yahoo.com/mRSS/’)意味着“将http://search.yahoo.com/mrss/的所有子元素检索为命名空间URI;另一种 – 最容易理解的 – 语法是:$news-> children(‘media’,True)(True表示“被视为前缀”).

回到示例中的代码,检索具有前缀media的所有第一个项目的子代的通用语法是:

$xml = simplexml_load_file( 'http://RSSFeeds.webmd.com/RSS/RSS.aspx?RSSSource=RSS_PUBLIC' );
$xml->channel->item[0]->children( 'http://search.yahoo.com/mRSS/' );

或(相同的结果):

$xml = simplexml_load_file( 'http://RSSFeeds.webmd.com/RSS/RSS.aspx?RSSSource=RSS_PUBLIC' );
$xml->channel->item[0]->children( 'media',True );

你的新代码

如果您想要显示< media:content url>页面中每个元素的缩略图,以这种方式修改原始代码

(...)
$pubDate = $xml->channel->item[$i]->pubDate;
$image   = $xml->channel->item[$i]->children( 'media',True )->content->attributes()['url'];
// in PHP < 5.5:
// $attr  = $xml->channel->item[$i]->children( 'media',True )->content->attributes();
// $image = $attr['url'];

$html   .= "<a href='$link'><h3>$title</h3></a>";
$html   .= "<img src='$image' alt='$title'>";
(...)

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念