douguyi3903 2013-01-31 15:40
浏览 85
已采纳

如何获取getElementsByTagNameNS内容?

I am trying to read and parse an RSS feed. The header of the RSS feed is the following:

<rss version="2.0" xmlns:content="http://www.mywebsite.com" xmlns:media="http://search.yahoo.com/mrss/" >

I successfully retrieved both <title> and <link> from items but I can't seem to find a way to retrieve <content:encoded> childs. That's what I tryed so far:

<?php

//error_reporting(E_ALL);
//ini_set("display_errors", 1);

....

$x=$xmlDoc->getElementsByTagName('item');

foreach($x as $item) {
    $link = $item->getElementsByTagName('link')->item(0)->nodeValue;
    $title = $item->getElementsByTagName('title')->item(0)->nodeValue;
    $content = $item->getElementsByTagNameNS('http://www.mywebsite.com', 'encoded')->item(0);
    echo ("<p><a href='" . $link. "'>" . $title . "</a></p><p>" . $content . "</p>");
}

?>

As you can imagine $content is empty even if I try to get the nodeValue attribute, what am I missing here?

  • 写回答

2条回答 默认 最新

  • dongtuan1980 2013-02-01 08:36
    关注

    I ended up using SimpleXmlElement:

    $x = new SimpleXmlElement($s);
    
    foreach($x->channel->item as $entry) {
    
        $content = $entry->children("content", true);
        echo "<p>".$content->encoded."</p>";
    }
    

    Works like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用