0

我正在使用 GoogleFeed API 将 RSS 提要显示为 HTML:

var feedcontainer=document.getElementById("feeddiv")
var feedurl="MY FEED URL"
var feedlimit=20
var rssoutput=""

function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method
feedpointer.setResultFormat(google.feeds.Feed.MIXED_FORMAT)
feedpointer.setNumEntries(feedlimit) //Google Feed API method
feedpointer.load(displayfeed) //Google Feed API method
}

function displayfeed(result){
if (!result.error){
var thefeeds=result.feed.entries
for (var i=0; i<thefeeds.length; i++)
rssoutput+="<a href='" + thefeeds[i].link + "' style='text-decoration:none'>" + "<h1 class='pub'>" + thefeeds[i].title + "</h1>" + "</a>" + new Date(thefeeds[i].publishedDate) + thefeeds[i].content + "<p class='text-small-hilite'><a href='" +thefeeds[i].link + "'>" + "Read full post" + "</a></p>"
rssoutput+=""
feedcontainer.innerHTML=rssoutput
}
else
alert("Error fetching feeds!")
}

window.onload=function(){
rssfeedsetup()
}

我还想在我的 tag 中将 CDATA 元素显示为 HTML。我试过 .xmlNode.getElementsByTagName 但没有奏效。

<item>
<title>
MY TITLE
</title>
<link>MY URL</link>
<comments>MY URL</comments>
 <pubDate>Fri, 17 Oct 2014 19:06:53 +0000</pubDate>
 <dc:creator>
 <![CDATA[ MY AUTHOR ]]>
 </dc:creator>

是否可以将 CDATA“我的作者”中的文本显示为 HTML?

4

1 回答 1

0

该标记具有 dc 命名空间,因此您可以在 for 循环中使用getElementsByTagNameNS方法,如下所示:

var creator = thefeeds[i].xmlNode.getElementsByTagNameNS(
              'http://purl.org/dc/elements/1.1/', 'creator')[0].innerHTML;

但如果你只想要 dc:creator 的内容,它可能与thefeeds[i].author. Feed API的JSON 文档会很有帮助。

提示:使用浏览器的开发工具并在 for 循环中设置断点,然后您可以查看对象包含的内容:

在此处输入图像描述

于 2014-11-01T11:23:30.587 回答