如何使用SimpleXML获取带有命名空间的节点的属性?

Jon*_*han 3 php xml namespaces simplexml

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>
Run Code Online (Sandbox Code Playgroud)

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 :(
}   
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 7

你在问题得到的代码确实有效.您已经正确地写过,您可以通过使用带有XML命名空间相关参数的SimpleXMLElement::children()方法,从不在默认命名空间中的namespaced元素作为根元素访问属性,$ns并且$is_prefix:

$youtube->entry->children('yt', TRUE)->duration->attributes()->seconds; // is "1870"
Run Code Online (Sandbox Code Playgroud)

由于这与您在问题中的基本相同,可以说您已经回答了自己的问题.与扩展的在线演示#2进行比较.


答案长:你在问题得到的代码确实有效.您可以在此处在线交互式示例中找到示例XML和代码:在线演示#1 - 它显示不同PHP和LIBXML版本的结果.

码:

$buffer = '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <entry>
        <yt:duration seconds="1870"/>
    </entry>
</feed>';

$xml = new SimpleXMLElement($buffer);

echo "ibxml version: ", LIBXML_DOTTED_VERSION, "\n";

foreach ($xml->entry as $item)
{
    //original comment: how to get seconds?
    $namespaces = $item->getNameSpaces(true);
    $yt         = $item->children($namespaces['yt']);
    $seconds    = $yt->duration->attributes();

    echo $seconds['seconds'], "\n"; // original comment: but doesn't work.
}

echo "done. should read 1870 one time.\n";
Run Code Online (Sandbox Code Playgroud)

结果:

输出为5.3.26,5.4.16 - 5.5.0

ibxml version: 2.9.1
1870
done. should read 1870 one time.
Run Code Online (Sandbox Code Playgroud)

输出为5.3.15 - 5.3.24,5.4.5 - 5.4.15

ibxml version: 2.8.0
1870
done. should read 1870 one time.
Run Code Online (Sandbox Code Playgroud)

输出为5.1.2 - 5.3.14,5.4.0 - 5.4.4

ibxml version: 2.7.8
1870
done. should read 1870 one time.
Run Code Online (Sandbox Code Playgroud)

从这个角度看,一切都很好.由于您没有给出任何具体的错误描述,因此很难说您的案例出了什么问题.您可能正在使用在您提出问题时过时的PHP版本,例如收到致命错误:

致命错误:调用未定义的方法SimpleXMLElement :: getNameSpaces()

可能也是由于过时的libxml版本.根据测试,以下libxml版本适用于PHP 5.1.2-5.5.0:

  • ibxml版本:2.9.1
  • ibxml版本:2.8.0
  • ibxml版本:2.7.8


Jon*_*han 4

所以我找到了一种使用 xpath 来做到这一点的方法,这是最好的方法还是有一种与我在问题中的代码一致的方法?只是出于好奇。

$source = 'youtube.xml';

// load as file
$youtube = new SimpleXMLElement($source, null, true);
$youtube->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007');

$count = 0;
foreach($youtube->entry as $item){

    //title works
    echo $item->title;

    $attributes = $item->xpath('//yt:duration/@seconds');
    echo $attributes[$count]['seconds'];
    $count++;
}
Run Code Online (Sandbox Code Playgroud)