Monday, 9 September 2013

Parsing WordPress RSS feed with Google API

Parsing WordPress RSS feed with Google API

I'm trying to get the two most recent blog entries from a WP blog with the
RSS feed. But I'm having trouble with the formatting of the text in the
output. It's just text, not HTML, and it shows all of the HTML formatting.
I'm using the Google Feed API, as such:
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("MY FEED URL");
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < 2; i++) {
var entry = result.feed.entries[i];
var div = document.createElement("div");
div.appendChild(document.createTextNode(entry.title));
var contentDiv = document.createElement("div");
contentDiv.setAttribute("id", "content-div");
contentDiv.appendChild(document.createTextNode(entry.content));
container.appendChild(div);
container.appendChild(contentDiv);
}
}
});
}
google.setOnLoadCallback(initialize);
</script>
But this outputs entry.content with loads of HTML tags in the text. The
Google API says the following:
entry.content: The body of this entry, including HTML tags. Since this
value can contain
HTML tags, you should display this value using elem.innerHTML =
entry.content (as opposed
to using document.createTextNode).
I'm not sure how to use elem.innerHTML in this context-- can anyone help?
Overall, I'd like to generate a div with the entry title and the content.
Thanks!

No comments:

Post a Comment