3

我有一个 XML 文档,它使用斜杠:注释。当我使用 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’&gt;<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?

4

1 回答 1

7

如果您尝试读取包含命名空间的标签,您将使用getElementsByTagNamesNS函数。从该提要的 XML 来看,其内容类似于:

$slashNS = "http://purl.org/rss/1.0/modules/slash/";
echo $node->getElementsByTagNameNS($slashNS, 'comments')->item(0)->nodeValue;
于 2012-06-09T17:31:23.693 回答