由于冒号而未获取 xml 节点


Not getting xml node because of colon

我有一个XML文档,它使用slash:comments。当我使用 php 尝试检索这些节点中的内容时,它只是不起作用。

这是我的代码:

    <?php
$rss = new DOMDocument();
$rss->load('http://terrodactyl.netau.net/wordpress/?feed=rss2');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                    'commentcount' => $node->getElementsByTagName('slash:comments')->item(0)->nodeValue,
                    'creator' => $node->getElementsByTagName('dc:creator')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
            $creator = $feed[$x]['creator'];
            $comments = $feed[$x]['commentcount'];
    echo "<div class=’blogpost primary_wide4’><h2>";
            echo "<h2>".$title."</h2><img class='left' src='images/image02.jpg' width='250' height='272' alt='' />";
    echo "<h3>Posted on ".$date."</h3>";
    echo "<p>".$description."</p>";
            echo "<p class='meta'>";
            echo "<span class='comments'><a href='".$link."'>".$comments."Comments</a></span>  ";
            echo " <span class='readmore'><a href='".$link."'> View Post</a></span></p>";
            echo "</div><br><br>";
}
?>

如何访问数据(在本例中为 0):

<slash:comments>0</slash:comments>

使用PHP?

如果您尝试读取包含命名空间的标记,则可以使用 getElementsByTagNamesNS 函数。从该提要的XML来看,其内容如下:

$slashNS = "http://purl.org/rss/1.0/modules/slash/";
echo $node->getElementsByTagNameNS($slashNS, 'comments')->item(0)->nodeValue;