How can i get to xml attribute of <yt:accesscontrol>?

坚强是说给别人听的谎言 提交于 2019-12-24 20:22:35

问题


I can't figure out or find out how to parse the permission="allowed" value out of this xml, using PHP simplexml_load_file.

the basic structure is

<?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" />

How can get to the value of the permission=allowed attribute on that last line?


回答1:


You want to use XPath to retrieve records, it's an XML query language.

Please see the SimpleXMLElement's xpath() and registerXPathNamespace() methods. W3Schools explains XPath's syntax here.

For this XML

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

you'd register a namespace like this:

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



回答2:


This was so annoying to figure out...

(where $xml = simplexml_load_file($source); )

I'm able to get to the permission attribute with:

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


来源:https://stackoverflow.com/questions/12504612/how-can-i-get-to-xml-attribute-of-ytaccesscontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!