【发布时间】:2011-09-28 10:32:30
【问题描述】:
youtube.xml
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
<entry>
...
<yt:duration seconds="1870"/>
...
</entry>
</feed>
update_videos.php
$source = 'youtube.xml';
// load as file
$youtube = new SimpleXMLElement($source, null, true);
foreach($youtube->entry as $item){
//title works
echo $item->title;
//now how to get seconds? My attempt...
$namespaces = $item->getNameSpaces(true);
$yt = $item->children($namespaces['yt']);
$seconds = $yt->duration->attributes();
echo $seconds['seconds'];
//but doesn't work :(
}
【问题讨论】:
-
@hakre,不确定它是否重复。我看不到提问者在哪里询问如何获取具有命名空间的节点的
attribute。 -
只能复制。我认为您的问题是有效的,我想知道为什么很难找到这些信息。 stackoverflow.com/questions/3438900/… 并检查:blog.sherifmansour.com/?p=302 - 只是在谷歌上搜索 simplexml 和命名空间
-
问题中的代码现在似乎可以按预期工作,至少在 PHP 5.2 和this testbed 中是这样。问题可能是
seconds属性不被认为在yt命名空间中,即使duration元素是as discussed elsewhere。这需要您选择NULL命名空间来获取属性,这可能在某些旧版本的 PHP 中不起作用? -
@IMSoP:I also can confirm your findings。我的结论如下: OP 没有提供任何体面的错误描述,所以很难说到底是什么问题。只能假设,所以我建议在提供更多信息之前搁置这个问题。如果可能的话,那么长时间之后就好了。如果不是,这个问题需要降级,因为它没有具体说明它的要求,并且阻碍了它所涉及的常见问题。
标签: php xml namespaces simplexml