开放API成了当下社区里的热点,一直以来很想备份一下自己在QQ空间里面的文章, 然而腾讯却不开放相关的API,所以只好另辟蹊径了。在网上搜索了下Qzone和rss,没想到还 真有结果,原来QQ空间的最新文章列表都存储在下面这个xml文件: http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=YourQQNumber 利用DOM读取xml文件的内容在.net framework中得到了很好的支持,恐怕很多 人都会,但对于C++来说,只能使用COM来解析xml文件。下面这个程序是我利用COM和Win32 SDK来实现的,将QQ空间中的文章备份为html格式。程序的主界面如下:
经过备份后的日志是这样的:
下面说道说道这个程序的实现过程: 首先针对Qzone xml文件的格式来封转一个xml文件读取类:
class QzoneItem;
typedef pair<string,string> ItemElem;
class QzoneXMLDoc
{
private:
IXMLDOMDocument *_pDoc;
public:
QzoneXMLDoc();
~QzoneXMLDoc();
void Load(string url);
void RetriveItem(vector<QzoneItem>&);
};
class QzoneItem
{
private:
vector< pair<string,string> > _Item;
public:
QzoneItem();
~QzoneItem();
void GetItem(string name,string content);
ItemElem& operator[](int index);
string& Title();
string& Link();
string& Description();
string& Category();
string& Author();
string& PubDate();
};
其中Load方法用来载入xml文件 RetriveItem将Qzone中每篇日志的内容存入vector<QzoneItem>中 QzoneItem类内部存储的是pair<string,string>类型,pair.first存储的是文章中信息类型 的名称,pair.second中存储的是实际的信息内容 有了上面的支撑类,下面就可以直接编程了,核心的实现是下面这个函数:
void CreateBackup(QzoneItem qVec)
{
ifstream template_file("template.html",ios_base::binary);
template_file.seekg(0,ios_base::end);
unsigned long length = template_file.tellg();
template_file.seekg(0,ios_base::beg);
vector<char> vTemp(length);
template_file.read(&vTemp[0],length);
string template_str(&vTemp[0], length);
replace(template_str,"%Title%",qVec.Title());
replace(template_str,"%Link%",qVec.Link());
replace(template_str,"%Description%",qVec.Description());
string file_name = qVec.Title() + ".html";
ofstream BackFile(file_name.c_str());
BackFile << template_str;
}
首先将模板template读入,然后将相关的字符串替换为从xml中读取到的日志内容,并用日志 标题存储为html文件,篇幅所限就不罗列所有代码了。