今天无意中发现了MetaWeblog的实现,了解了一下相关知识,也学习了一下xml-rpc的知识。现阶段在学习ajax,测试用javascript调用了一下MetaWeblog函数,学习一下。
输入博客园的用户名和密码,点击"获取最新Blogs"获取Blog。
上面的多行文本框是发送到服务器端的XML片段,下面的多行文本框是服务器返回的XML。然后用xslt对返回的xml转换为可读格式。
代码如下: 下载
<HTML>
<HEAD>
<TITLE> MetaWeblog - By pharaoh.cnblogs.com</TITLE>
<META NAME="Author" CONTENT="Pharaoh168@gmail.com">
</HEAD>
<BODY>
<div id=load style="display:none; position:absolute;right:0px;top:0px;background:#FF5B5B;border:1px solid">正在加载</div>
用户名:<input id=username value=''>
密码:<input type=password id=password value=''>
BlogID:<input id=blogid value=''><button onclick="getUsersBlogs();">获取BlogID</button>
<textarea style="100%;height:50px;" readonly id=sdtext></textarea>
<textarea style="100%;height:100px;" readonly id=gttext></textarea>
获取个数:<input id=getcount style="20px;" value = '5'><button onclick="getRecentPosts();">获取最新Blogs</button>
<div id='blogs'></div>
<xml id=xslt>
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/methodResponse/params/param/value/array/data">
<xsl:for-each select="value/struct">
<div style="background:#EEEEEE"><a href="{member[6]/value/string}" target="_blank"> <xsl:value-of select="member[3]/value/string"/></a> <xsl:value-of select="member/value/dateTime.iso8601"/></div>
<div><xsl:value-of select="member[2]/value/string" disable-output-escaping="yes" /></div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</xml>
<script language="javascript">
MetaWeblog = function(methodName,rsFunction)
{
this.fnResult = rsFunction;
var methodname = methodName;
var ptext = '';
this.Add = function(v,tp) {
if (!tp) tp='string';
ptext += "<param><value><" +tp+ ">"+ v +"</"+tp+"></value></param>";
}
this.GetParamsText = function() {
return '<?xml version="1.0"?><methodCall><methodName>'+methodname+"</methodName><params>"+ptext+"</params></methodCall>";
}
this.clear = function() {
ptext = '';
}
}
function $(id) { return document.getElementById(id); }
function getUsersBlogs()
{
var blogid = new MetaWeblog('blogger.getUsersBlogs',rsgetUsersBlogs);
blogid.Add('123456798');
blogid.Add($('username').value);
blogid.Add($('password').value);
GetResponseXML(blogid);
}
function rsgetUsersBlogs(oXml)
{
var node = oXml.selectSingleNode('/methodResponse/params/param/value/array/data/value/struct/member/value/string');
$('blogid').value = node.text;
}
function getRecentPosts()
{
var blog = new MetaWeblog('metaWeblog.getRecentPosts',rsgetRecentPosts);
blog.Add($('blogid').value);
blog.Add($('username').value);
blog.Add($('password').value);
blog.Add($('getcount').value,'int');
GetResponseXML(blog);
}
function rsgetRecentPosts(oXml)
{
var txt = oXml.transformNode(xslt.documentElement);
$('blogs').innerHTML = txt;
}
var xmlhttp ;
function GetResponseXML(MWeblog)
{
var metaWeblog = MWeblog;
load.style.display='';
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.onreadystatechange = function() {
window.status += xmlhttp.readyState+' ';
if(xmlhttp.readyState == 4)
{
load.style.display='none';
window.status = '获取数据结束。';
if(xmlhttp.status == 200)
{
var xmlReturn = xmlhttp.responseText;
$("gttext").value=xmlReturn;
metaWeblog.fnResult(xmlhttp.responseXML);
}
else
{
alert("出现错误:\n"+xmlhttp.statusText+"\n"+xmlhttp.status);
}
xmlhttp = null;
}
}
//var murl = 'http://pharaoh.cnblogs.com/services/metablogapi.aspx';
var murl='http://'+$('username').value+'.cnblogs.com/services/metablogapi.aspx'
xmlhttp.Open("POST",murl,true);
var sendText = metaWeblog.GetParamsText();
$('sdtext').value = sendText;
xmlhttp.Send(sendText);
window.status = '开始获取';
}
</script>
</BODY>
</HTML>