Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SimpleXML PHP访问XML中的命名空间冒号节点_Php_Xml_Parsing_Rss_Simplexml - Fatal编程技术网

使用SimpleXML PHP访问XML中的命名空间冒号节点

使用SimpleXML PHP访问XML中的命名空间冒号节点,php,xml,parsing,rss,simplexml,Php,Xml,Parsing,Rss,Simplexml,这类似于我发布的一个问题,但我扩展了范围,因为这必须是可以解决的 我正在尝试从这个rss源访问分级和视图计数。现在,这些名称空间使用yt:和gd:-命名,但是名称空间引用是来自youtube提要的404,所以它似乎失败了。需要帮忙吗 我正在尝试使用:- $ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics->attributes('viewCount')); 但它失败了。。。它通常是这样工

这类似于我发布的一个问题,但我扩展了范围,因为这必须是可以解决的

我正在尝试从这个rss源访问分级和视图计数。现在,这些名称空间使用yt:和gd:-命名,但是名称空间引用是来自youtube提要的404,所以它似乎失败了。需要帮忙吗

我正在尝试使用:-

$ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics->attributes('viewCount'));
但它失败了。。。它通常是这样工作的,例如,我可以访问媒体:使用

$ytFeed->children('http://search.yahoo.com/mrss/')->group->category);
这是原始RSS,请查看底部的这些节点:-

<?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'>
    <id>http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E</id>
    <published>2009-03-02T07:02:49.000Z</published>
    <updated>2011-07-19T06:57:29.000Z</updated>
    <category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video' />
    <category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Music' label='Music' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Other Lives' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jesse Tabish' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jenny Hsu' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jupiter Hotel' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Portland' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='music' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='band' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='interview' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='mf magazine' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='fashion' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='hotel room' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='doug fir lounge' />
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='erik schultz' />
    <title type='text'>Other Lives</title>
    <content type='text'>We sat down with Jesse Tabish and Jenny Hsu from Other Lives at the Jupiter Hotel in Portland, OR. We love these guys. Check them out at myspace.com/otherlivesVisit mf magazine at musicfashionmagazine.com or myspace.com/musicfashion</content>
    <link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=tDJFDsZFw2E&amp;feature=youtube_gdata' />
    <link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/responses' />
    <link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/related' />
    <link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E' />
    <author>
        <name>mfmagazine</name>
        <uri>http://gdata.youtube.com/feeds/api/users/mfmagazine</uri>
        </author>
    <gd:comments>
        <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/comments' countHint='16' />
        </gd:comments>
    <media:group>
        <media:category label='Music' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Music</media:category>
        <media:content url='http://www.youtube.com/v/tDJFDsZFw2E?f=videos&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='600' yt:format='5' />
        <media:content url='rtsp://v3.cache3.c.youtube.com/CiILENy73wIaGQlhw0XGDkUytBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='600' yt:format='1' />
        <media:content url='rtsp://v6.cache3.c.youtube.com/CiILENy73wIaGQlhw0XGDkUytBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='600' yt:format='6' />
        <media:description type='plain'>We sat down with Jesse Tabish and Jenny Hsu from Other Lives at the Jupiter Hotel in Portland, OR. We love these guys. Check them out at myspace.com/otherlivesVisit mf magazine at musicfashionmagazine.com or myspace.com/musicfashion</media:description>
        <media:keywords>Other Lives, Jesse Tabish, Jenny Hsu, Jupiter Hotel, Portland, music, band, interview, mf magazine, fashion, hotel room, doug fir lounge, erik schultz</media:keywords>
        <media:player url='http://www.youtube.com/watch?v=tDJFDsZFw2E&amp;feature=youtube_gdata_player' />
        <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/0.jpg' height='240' width='320' time='00:05:00' />
        <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/1.jpg' height='90' width='120' time='00:02:30' />
        <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/2.jpg' height='90' width='120' time='00:05:00' />
        <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/3.jpg' height='90' width='120' time='00:07:30' />
        <media:title type='plain'>Other Lives</media:title>
        <yt:duration seconds='600' />
        </media:group>
    <gd:rating average='5.0' max='5' min='1' numRaters='17' rel='http://schemas.google.com/g/2005#overall' />
    <yt:statistics favoriteCount='10' viewCount='3572' />
    </entry>

http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E
2009-03-02T07:02:49.000Z
2011-07-19T06:57:29.000Z
其他生命
我们和杰西·塔比什和詹妮·许在波特兰的朱庇特酒店坐了下来。我们爱这些家伙。请访问myspace.com/otherlivesVisit mf杂志musicfashionmagazine.com或myspace.com/musicfashion查看
mfmagazine
http://gdata.youtube.com/feeds/api/users/mfmagazine
音乐
我们和杰西·塔比什和詹妮·许在波特兰的朱庇特酒店坐了下来。我们爱这些家伙。请访问myspace.com/otherlivesVisit mf杂志musicfashionmagazine.com或myspace.com/musicfashion查看
其他生活,杰西·塔比什,詹妮·许,木星酒店,波特兰,音乐,乐队,采访,mf杂志,时尚,酒店房间,道格·菲尔休息室,埃里克·舒尔茨
其他生命

调用子函数时简单地传递名称空间参数

$nodes = $xml->children('gd', true);
请参阅手册:

使用和:


好的,第一个问题是你误用了这个方法:

在上面的示例中,您传入了所需属性的名称,该名称不是
attributes
接受的参数。相反,上面的方法将返回
statistics
元素的所有属性,其中属性本身的命名空间为
viewCount
,因为该方法希望第一个参数(如果设置了)是属性的命名空间

如果您改为以元素数组的形式访问属性,则代码将正常工作,如下所示:

$ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics['viewCount'];
如果要避免每次都使用完整的命名空间uri和
子类
方法,可以通过使用
getNamespaces
方法将所有命名空间映射到一个数组,然后通过
子类
方法将具有该命名空间的子类映射到一个对象来简化,如:

$namespaces = $ytFeed->getNameSpaces(true);
$yt = $ytFeed->children($namespaces['yt']);
$yt->statistics['viewCount'];

// Access all media: namespaced group elements like in your "working" example:
$media = $ytFeed->children($namespaces['media']);
$media->group->category;

我意识到这已经超过2年了,一个答案被接受了,但被接受的答案实际上只是一个更好记录和更直观的替代方案,但没有告诉发现这个问题的其他人使用SimpleXML实现这一点的实际方法,这最终只需要在DOMDocument的6-8行代码的基础上增加两行代码和一个不同的扩展,这可能会误导未来的读者,使他们认为不能用SimpleXML来完成。它可以,而且很简单,只是不太明显。

我的可能副本扩大了,并将其删除。我在一开始就这样说过。如果有人遇到这一点,答案中没有涉及到一个要点:名称空间的URI不需要以任何方式进行检索。它不是对XSD或其他定义的引用,只是一个任意标识符,它使用域的所有权来确保没有任何重复项。但是,无法处理我尝试的上述数据。我有RTFM,但这是一个一致的问题,尚未解决。-我得到“SimpleXMLElement::children()最多需要1个参数,2个给定参数”@Wise驴,正如您在手册页面上看到的,is_前缀仅随PHP5.2一起添加,因此这意味着您的PHP版本比它旧。强烈建议您更新您正在使用的PHP的任何版本,因为任何<5.3的版本都不再受支持。对,感谢指针-不可能更新atm。没有别的办法吗?这有点让人头疼,但它确实有效,所以被采用了。谢谢除非您使用的是非常古老的PHP版本,否则不再需要使用
getnamespace
技巧,因为
children()
attributes()
现在都可以通过提供第二个参数
true
直接指定前缀。依赖前缀(无论哪种方式)都意味着使用的前缀发生了变化(但这不会改变代码的“含义”。最好只为您知道将要使用的名称空间使用自己的名称构建一个数组(这是URI部分,如
)http://gdata.youtube.com/schemas/2007“
保证不会更改)。
$ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics['viewCount'];
$namespaces = $ytFeed->getNameSpaces(true);
$yt = $ytFeed->children($namespaces['yt']);
$yt->statistics['viewCount'];

// Access all media: namespaced group elements like in your "working" example:
$media = $ytFeed->children($namespaces['media']);
$media->group->category;