Created
March 1, 2011 09:46
-
-
Save yyr446/848909 to your computer and use it in GitHub Desktop.
rdfで定義されたRSSのXMLを「Google Feed API」で取り込んでXpathで弄る
Google Feed API Google Feed API Google Feed API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>記事一覧</title> | |
<style type="text/css"> | |
//体裁なんかいちいち作るかって! | |
</style> | |
<script type="text/javascript" src="http://www.google.com/jsapi"></script> | |
<script type="text/javascript">google.load("feeds","1");</script> | |
<script type="text/javascript"> | |
function SimpleRssReader(rss_url,kensu,container_id){ | |
var kensu = kensu?kensu:10; | |
var container = document.getElementById(container_id); | |
var feed = new google.feeds.Feed(rss_url); | |
feed.setNumEntries(kensu); | |
feed.setResultFormat(google.feeds.Feed.XML_FORMAT); | |
feed.load(handler); | |
feed = null; | |
function handler(result){ | |
if(result.status.code=="200"){ | |
output(result.xmlDocument); | |
}else alert("RSS Error:" + result.status.code); | |
} | |
function nsResolver(prefix) { | |
var ns = { | |
"rss" : "http://purl.org/rss/1.0/", | |
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | |
"dc" : "http://purl.org/dc/elements/1.1/" | |
}; | |
return ns[prefix] || null; | |
} | |
function output(xmlDoc){ | |
var entries = []; | |
var itemIterator,titleIterator,dateIterator,linkIterator,descIterator; | |
var itemIterator = xmlDoc.evaluate('/rdf:RDF/rss:item',xmlDoc,nsResolver,XPathResult.ANY_TYPE,null); | |
var thisitem = itemIterator.iterateNext(); | |
var i = 0; | |
while (thisitem) { | |
titleIterator = xmlDoc.evaluate("./rss:title",thisitem,nsResolver,XPathResult.ANY_TYPE,null); | |
dateIterator = xmlDoc.evaluate("./dc:date",thisitem,nsResolver,XPathResult.ANY_TYPE,null); | |
linkIterator = xmlDoc.evaluate("./rss:link",thisitem,nsResolver,XPathResult.ANY_TYPE,null); | |
descIterator = xmlDoc.evaluate("./rss:description",thisitem,nsResolver,XPathResult.ANY_TYPE,null); | |
entries[i] = { | |
title:titleIterator.iterateNext().firstChild.nodeValue, | |
publishedDate:dateIterator.iterateNext().firstChild.nodeValue, | |
link:linkIterator.iterateNext().firstChild.nodeValue, | |
desc:descIterator.iterateNext().firstChild.nodeValue | |
}; | |
thisitem = itemIterator.iterateNext(); | |
i += 1; | |
} | |
if(container){ | |
var ul,title,link,d,youbi,publishedDate,li,a; | |
while(container.hasChildNodes()) | |
container.removeChild(container.firstChild); | |
ul = document.createElement("ul"); | |
ul.style.listStyleType = 'none'; | |
youbi = new Array("日","月","火","水","木","金","土"); | |
for(var i=0;i<entries.length;i++){ | |
title = entries[i].title; | |
link = entries[i].link | |
d = new Date(entries[i].publishedDate); | |
publishedDate = { | |
year:d.getFullYear(), | |
month:d.getMonth()+1, | |
date:d.getDate(), | |
youbi:youbi[d.getDay()], | |
hr:d.getHours(), | |
min:d.getMinutes(), | |
sec:d.getSeconds() | |
}; | |
li = document.createElement("li"); | |
a = document.createElement("a"); | |
a.href = link; | |
a.appendChild(document.createTextNode(title)); | |
li.appendChild(a); | |
li.appendChild(document.createTextNode( | |
" " + | |
publishedDate.year + "年" + | |
publishedDate.month + "月" + | |
publishedDate.date + "日" + | |
"(" + publishedDate.youbi + ")" + | |
publishedDate.hr + "時" + | |
publishedDate.min + "分" + | |
publishedDate.sec + "秒" | |
)); | |
ul.appendChild(li); | |
} | |
container.appendChild(ul); | |
}else return false; | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<h3>記事一覧</h3> | |
<div id="container">Loading...</div> | |
<script type="text/javascript"> | |
var myRssList = new SimpleRssReader("https://gazoo.com/G-Blog/tam/BlogRss.aspx",10,"container"); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
デモサイト
http://yoneyone.my-sv.net/test/GfeedRdr2.htm
動作確認:Firefox 3.6.13、GoogleChrome 9.0
IEは動きません(MSXML使えばきっとできるじゃろうて)