3

I have an XML document, and it uses slash:comments. When I use php to try and retrieve things within these nodes, it just doesn't work.

Here's my code:

    <?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>";
}
?>

How can i access the data (in this case, 0):

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

using php?

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 2
    "It does not work" - great explanation of your problem, I'm contacting Charles Xavier to help me figure out what "does not work" means for you. – hakre Jun 09 '12 at 17:05
  • 1
    possible duplicate of [PHP library for parsing XML with a colons in tag names?](http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names) – Michael Berkowski Jun 09 '12 at 17:06
  • 2
    possible duplicate of [Help parsing XML with DOMDocument](http://stackoverflow.com/questions/4902288/help-parsing-xml-with-domdocument) - that one is for Namespaces in Element Names and DOMDocument. – hakre Jun 09 '12 at 17:09

1 Answers1

7

You'd use the getElementsByTagNamesNS function if you are trying to read a tag that includes a namespace. Judging by the XML for that feed, that would read something like:

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