如何在RSS feed <site:content>中解析带有名称空间的标记

时间:2016-03-31 17:19:12

标签: php xml rss

我在使用包含名称空间解析RSS feed时遇到问题。我在这种情况下使用PHP,所有其他文件都被正确解析。

唯一有问题的是RSS Feed中的描述,此标记为<job:description>

任何建议将不胜感激!

<?php

    $rss = new DOMDocument();
    $rss->load('http://careers.pageuppeople.com/671/cw/en-us/rss');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
                    $item = array ( 
                                    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                                    'desc' => $node->getElementsByTagNameNS("http://careers.pageuppeople.com/671/cw/en-us/rss","description")->item(0)->nodeValue,
                                    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                                    'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                                    'closeDate' => $node->getElementsByTagName('closingDate')->item(0)->nodeValue,
                                    'field_city' => $node->getElementsByTagName('location')->item(0)->nodeValue,
                                    );
                    array_push($feed, $item);
    }
    $limit = 5;
    echo '<?xml/>';
    for($x=0;$x<$limit;$x++) {
                    echo '<item>';
                    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
                    $link = $feed[$x]['link'];
                    $description = $feed[$x]['desc'];
                    $field_city = $feed[$x]['field_city'];
                    $pubDate = date('Y: m: d', strtotime($feed[$x]['pubDate']));
                    $closeDate = date('Y: m: d', strtotime($feed[$x]['closeDate']));
                    echo '<title>'.$title.'</title>';
                    echo '<pubDate>'.$pubDate.'</pubDate>';
                    echo '<closeDate> '.$closeDate.'</closeDate>';
                    echo '<link>'.$link.'</link>';
                    echo '<field_city>'.$field_city.'</field_city>';
                    echo '<body>'.$description.'</body>';
                    echo '<field_how_to_apply><strong>UNICEF is committed to diversity and inclusion within its workforce, and encourages qualified female and male candidates from all national, religious and ethnic backgrounds, including persons living with disabilities, to apply to become a part of our organization.<br><br>To apply click on the link below.</strong><br><br>'.$link.'</field_how_to_apply>';
                    echo '</item>';

    }

    echo '</channel></rss>';

?>

1 个答案:

答案 0 :(得分:1)

您使用了错误的NameSpaceURI。您可以在名称空间节点的父节点中找到NameSpaceURIs搜索xmlns:prefix(通常在根节点中)。

在你的情况下:

<channel xmlns:job="http://pageuppeople.com/">

所以你必须使用正确的NSURI:

(...)
'desc' => $node->getElementsByTagNameNS("http://pageuppeople.com/","description")->item(0)->nodeValue,
(...)

你的脚本将会工作。