使用 PHP 访问名称中带有冒号的 XML 属性 [英] Accessing XML properties that have colon in the name with PHP

查看:36
本文介绍了使用 PHP 访问名称中带有冒号的 XML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 YouTube XML 供稿中获取视频信息,例如标题和缩略图 URL.

I'm trying to get video info from a YouTube XML feed such as the title and thumbnail URL.

让我们以这个提要为例:http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8

Let's take this feed as an example: http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8

这是我现在拥有的代码:

Here's the code I have now:

$videoURL = 'http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8';
$sxml = simplexml_load_file($videoURL);
print_r($sxml);

但是,这似乎不包括 xml 表的 media:group 部分.

However, that doesn't seem to be including the media:group part of the xml sheet.

本质上,我试图获得这两个值:

Essentially, I'm trying to get these two values:

entry -> media:group -> media:title
entry -> media:group -> media:thumbnail

这是我通过阅读其他类似问题最终得到的解决方案.

This is the solution I ended up going with from reading other similar questions.

$videoTitle = $sxml->children('media', true)->group->title;
$videoThumbnail = $sxml->children('media', true)->group->thumbnail->attributes()->url;

推荐答案

标签名称中的冒号将命名空间前缀与本地元素或属性名称分开.问题中的提要有多个命名空间.

The colon in a tag name separates the namespace prefix from the local element or attribute name. The feed in the question has several namespaces.

  • http://www.w3.org/2005/Atom
  • http://purl.org/atom/app# 带前缀 app
  • http://search.yahoo.com/mrss/ 带有前缀 media
  • http://schemas.google.com/g/2005 带有前缀 gd
  • http://gdata.youtube.com/schemas/2007 带有前缀 yt
  • http://www.w3.org/2005/Atom
  • http://purl.org/atom/app# with prefix app
  • http://search.yahoo.com/mrss/ with prefix media
  • http://schemas.google.com/g/2005 with prefix gd
  • http://gdata.youtube.com/schemas/2007 with prefix yt

命名空间前缀是实际命名空间的别名,它只对文档有效,对子节点可以改变.下一个提要可以为 http://www.w3.org/2005/Atom 使用前缀 atom.因此,依赖文档中的命名空间前缀是一个坏主意.

The namespace prefix is an alias for the actual namespace, it is only valid for the document and can change for child nodes. The next feed could use the prefix atom for http://www.w3.org/2005/Atom. So depending on the namespace prefix from the document is a bad idea.

如果您使用 Xpath 从文档中获取元素,您可以为使用的命名空间注册您自己的前缀.以下示例将媒体 RSS 标题和大缩略图的 url 直接作为字符串获取.如果元素不存在,它将返回一个空字符串.

If you use Xpath fo fetch the elements from a document, you can register your own prefixes for the used namespaces. The following example fetches the media rss title and the url of the large thumbnail directly as strings. If an element does not exists, it will return an empty string.

$videoURL = 'http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8';
$dom = new DOMDocument();
$dom->load($videoURL);

$xpath = new DOMXPath($dom);
$xpath->registerNamespace('mrss', 'http://search.yahoo.com/mrss/');

var_dump(
  $xpath->evaluate('string(//mrss:title)', NULL, FALSE),
  $xpath->evaluate('string(//mrss:thumbnail[@width="480"]/@url)', NULL, FALSE)
);

输出:

string(28) "Katy Perry - Roar (Official)"
string(40) "http://i1.ytimg.com/vi/CevxZvSJLk8/0.jpg"

这篇关于使用 PHP 访问名称中带有冒号的 XML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆