如何让Google Feed API显示CDATA?

时间:2014-10-30 18:16:46

标签: javascript rss cdata google-feed-api

我正在使用GoogleFeed API将RSS Feed显示为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()
}

我还希望在我的标签中以HTML格式显示CDATA元素。我已经尝试过.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“MY AUTHOR”中的文本显示为HTML?

1 个答案:

答案 0 :(得分: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's JSON documentation可能会有所帮助。

提示:使用您的browser's developer tools并在for循环中设置断点,然后您可以看到该对象包含的内容:

enter image description here