如何解析RSS提要中带有命名空间的标签<;site:content>;


How to parse tags with namespaces in RSS feed <site:content>

我在解析包含名称空间的RSS提要时遇到问题。我在这个场景中使用PHP,所有其他文件都得到了正确的解析。

唯一有问题的是RSS提要中的描述,这个标签是<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>';
?>

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

在您的情况下:

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

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

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

你的剧本会成功的。