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

我无法弄清楚或找出如何解析 permission =“allowed”</ code >使用PHP simplexml_load_file </ code>从这个xml中取出值。</ p>

基本结构是</ p>

 &lt;?  xml version ='1.0'coding ='UTF-8'?&gt; 
&lt; 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 / &amp; quot; DkEDSH47eCp7I2A9WhJbEEQ。&amp; quot;“&gt;
&lt; yt:accessControl action =”comment“permission =”allowed“/&gt;
&lt; yt:accessControl action =”commentVote“permission =”allowed“/&gt ;
&lt; yt:accessControl action =“videoRespond”permission =“moderated”/&gt;
&lt; yt:accessControl action =“rate”permission =“allowed”/&gt;
&lt; yt:accessControl action =“embed” permission =“allowed”/&gt;
&lt; yt:accessControl action =“list”permission =“allowed”/&gt;
&lt; yt:accessControl action =“autoPlay”permission =“allowed” /&gt;
&lt; yt:accessControl action =“syndicate”permission =“allowed”/&gt;
</ code> </ pre>

如何获取的值 最后一行上的permission = allowed </ code>属性?</ p>
</ div>

展开原文

原文

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?

2个回答



这太烦人了...... </ p>

(其中 $ xml = simplexml_load_file($ source); </ code>)</ p>

我可以通过以下方式获取权限属性:</ p>

  $  XML-&GT;儿童( 'http://gdata.youtube.com/schemas/2007') - &GT; AccessControl的[4]  - &GT;属性() - &GT;许可; 
</代码> </ PRE>

</ div>

展开原文

原文

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;

dook0034
dook0034 我认为这只是为了了解XML中存在名称空间并且SimpleXMLElement支持这些名称空间。 另一种获取这些方法的方法是在调用simplexml_load_file btw时指定命名空间,参见什么是“$ ns”和“$ is_prefix”参数? 以及SimpleXML中getDocNamespaces和getNamespaces之间的区别。
大约 7 年之前 回复



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

请参阅 SimpleXMLElement的xpath() registerXPathNamespace()方法。
W3Schools解释 XPath的语法。</ p>

对于此XML </ p>

 <  code> $ xml =&lt;&lt;&lt; EOD 
&lt; book xmlns:chap =“http://example.org/chapter-title”&gt;
&lt; title&gt; My Book&lt; / title&gt;
&lt; / book&gt;
EOD;
</ code> </ pre>

您将注册这样的命名空间:</ p>

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

展开原文

原文

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');

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐