我怎样才能获得<yt:accesscontrol>的xml属性?</yt:accesscontrol>

时间:2012-09-20 00:21:06

标签: php xml youtube simplexml xml-namespaces

我无法弄清楚或了解如何使用PHP permission="allowed"解析此xml中的simplexml_load_file值。

基本结构是

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/&quot;DkEDSH47eCp7I2A9WhJbEEQ.&quot;">
<yt:accessControl action="comment" permission="allowed" />
<yt:accessControl action="commentVote" permission="allowed" />
<yt:accessControl action="videoRespond" permission="moderated" />
<yt:accessControl action="rate" permission="allowed" />
<yt:accessControl action="embed" permission="allowed" />
<yt:accessControl action="list" permission="allowed" />
<yt:accessControl action="autoPlay" permission="allowed" />
<yt:accessControl action="syndicate" permission="allowed" />

如何获取最后一行permission=allowed属性的值?

2 个答案:

答案 0 :(得分:2)

您想使用 XPath 来检索记录,它是一种XML查询语言。

请参阅SimpleXMLElement's xpath()registerXPathNamespace()方法。 W3Schools解释XPath's syntax here

对于此XML

$xml = <<<EOD
<book xmlns:chap="http://example.org/chapter-title">
   <title>My Book</title>
</book>
EOD;

你要注册这样的命名空间:

$sxe = new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('c', 'http://example.org/chapter-title');
$result = $sxe->xpath('//c:title');

答案 1 :(得分:0)

这太令人讨厌了......

$xml = simplexml_load_file($source);

我可以使用以下命令获取权限属性:

$xml->children('http://gdata.youtube.com/schemas/2007')->accessControl[4]->attributes()->permission;