如何显示特定的 XML 节点属性


How to display a specific XML node attribute?

我想在我的网站上创建一个简单的"新闻"小部件,链接到我的Squarespace博客的RSS提要。

RSS提要可以在这里找到: https://weboxsite.squarespace.com/?format=rss

当我从 CURL 函数加载 XML 数据时,当我print_r() 我的 XML 文件时,我无法看到某些节点。

最特别的是,我想<media>获取节点属性。

当涉及到获取我的<media>节点的属性时,我无法获取url属性。

我简化了网络的结果,以便更轻松地阅读。

<item>
<title>Google Disque : un outil indispensable</title>
<category>Google drive</category>
<dc:creator></dc:creator>
<pubDate>Wed, 22 Jun 2016 21:25:37 +0000</pubDate>
<link>
http://blogue.webox.site/touslesarticles/2016/6/22/google-disque-un-outil-indispensable
</link>
<guid isPermaLink="false">
5769a85b9de4bbf4535c1896:5769a8f1bebafb833a859939:576b01e48419c2d2589b7264
</guid>
<description>
My excerpt....
</description>
<content:encoded>
<![CDATA[
<p>My content....</p> 
]]>
</content:encoded>
<media:content type="image/jpeg" url="http://static1.squarespace.com/static/5769a85b9de4bbf4535c1896/5769a8f1bebafb833a859939/576b01e48419c2d2589b7264/1466630737869/1500w/googledisque_bg.jpg" medium="image" isDefault="true" width="510" height="334">
<media:title type="plain">Google Disque : un outil indispensable</media:title>
</media:content>
</item>
**

主要问题 **

<media:content type="image/jpeg" url="http://static1.squarespace.com/static/5769a85b9de4bbf4535c1896/5769a8f1bebafb833a859939/576b01e48419c2d2589b7264/1466630737869/1500w/googledisque_bg.jpg" medium="image" isDefault="true" width="510" height="334">
    <media:title type="plain">Google Disque : un outil indispensable</media:title>
    </media:content>

这是我到目前为止的代码

<?php 
$limit = 4;
    $c=curl_init('https://weboxsite.squarespace.com/?format=rss');
    curl_setopt( $c, CURLOPT_USERAGENT,'nesss' );
    curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
    $r=curl_exec( $c );
    curl_close( $c );
    $rss = new DOMDocument();
    $rss->loadxml($r);
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title'     =>  $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link'      =>  $node->getElementsByTagName('link')->item(0)->nodeValue,
            'media'     =>  $node->getElementsByTagName('media')->item(0)->nodeValue,
            'cat'       =>  $node->getElementsByTagName('category')->item(0)->nodeValue
        );
        array_push($feed, $item);
    }
    for($x = 0; $x < $limit; $x++) {
        $title  = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link   = $feed[$x]['link'];
        $desc   = $feed[$x]['media'];
        $cat   = $feed[$x]['cat'];
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></p>';
        echo '<p>'.$cat.'</p>';
    }
    ?>

我知道这样做'media' => $node->getElementsByTagName('media')->item(0)->nodeValue不是这样做的好方法,因为它试图呈现值。

我试图放置'media'=> $node->getElementsByTagName('media')->item(0)->getAttribute('url')但我收到错误。

Call to a member function getAttribute() on null in ...

我可能认为这是因为节点被命名为 media:content,不仅是媒体,而且发生了变化的事件,它无处可去。

我某处缺少什么吗?

具有 media: 的元素位于不同的 XML 命名空间中。因为你正在阅读应该是Media-RSS的RSS。查找属性xmlns:media="http://search.yahoo.com/mrss/" 。这是命名空间的定义。分析器将前缀解析为实际命名空间。

  • media:content -> {http://search.yahoo.com/mrss/}content
  • media:title -> {http://search.yahoo.com/mrss/}title

由于元素位于命名空间中,因此必须使用命名空间感知方法:

$title = $node->getElementsByTagNameNS(
  'http://search.yahoo.com/mrss/', 'title'
)->item(0)->nodeValue;

或者,您使用 Xpath 表达式并注册自己的前缀。

$rss = new DOMDocument();
$rss->loadxml($r);
$xpath = new DOMXpath($rss);
$xpath->registerNamespace('m', 'http://search.yahoo.com/mrss/');
$feed = array();
foreach ($xpath->evaluate('//item') as $node) {
    $feed[] = array( 
        'title' => $xpath->evaluate('string(title)', $node),
        'link' => $xpath->evaluate('string(link)', $node),
        'media-title' => $xpath->evaluate('string(m:content/m:title)', $node),
        'cat' => $xpath->evaluate('string(category)', $node)
    );
}

尝试使用:

getElementsByTagNameNS ( string $namespaceURI , string $localName )

这里是来自您文件的命名空间URI:

xmlns:content="http://purl.org/rss/1.0/modules/content/"

xmlns:wfw="http://wellformedweb.org/CommentAPI/"

xmlns:iTunes="http://www.itunes.com/dtds/podcast-1.0.dtd"

xmlns:dc="http://purl.org/dc/elements/1.1/"

xmlns:media="http://www.rssboard.org/media-rss"

所以最后:

'media' => $node->getElementsByTagName('content')->item(0)->nodeValue

成为

'media' => $node->getElementsByTagNameNS('http://www.rssboard.org/media-rss','content')->item(0)->getAttribute('url')

请记住:"NAMESPACE:NODENAME",所以你正在寻找content而不是media

希望有帮助。