tinyxml2解析RSS文件 并将其转换为.dat格式
来源:互联网 发布:诸神黄昏神翼进阶数据 编辑:程序博客网 时间:2024/06/06 09:42
参考博客:
tinyXml2 – https://github.com/leethomason/tinyxml2
rss – http://www.runoob.com/rss/rss-tutorial.htm
mxl格式 – http://blog.csdn.net/zhubinqiang/article/details/7185322
在c++中使用tinyxml2 解析XML格式文件例子 – http://blog.csdn.net/educast/article/details/12908455
tinyxml2 使用教程 – http://blog.csdn.net/K346K346/article/details/48750417
xmldocument 类 – http://www.cnblogs.com/kissdodog/archive/2013/02/24/2924236.html
参考接口:
struct RssItem{
string title;
string link;
string description;
string content;
};
class RssReader{
public:
RssReader();
void parseRss();//解析
void dump();//输出
private:
vector _rss;
};
要求:最后生成一个 pagelib.dat, 其格式:
<doc> <docid>1</docid> <title> ... </title> <link> ... </link> <content> ... </content> </doc> <doc> ... </doc> <doc> ... </doc>
rss 格式
<?xml version="1.0" encoding="utf-8" ?> //声明该文档是xml文档<rss version="2.0"> //声明当前文件为rss格式文件<channel> <title></title> //对网站和当前rss进行描述 <description>国内焦点新闻列表</description> //对当前rss进行描述<link></link><item>//一条信息<title>最高检:严惩公务员利用审批等权力索贿受贿</title><link></link>//新闻链接<description><description>//新闻描述</item></channel></rss>
以下是可能用到的一些函数的准备
FirstChildElement(const char* value=0): 获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。
RootElement(): 获取根节点,相当于FirstChildElement的空参数版本;
const XMLAttribute* FirstAttribute() const: 获取第一个属性值;
Attribute(“Password”) //获取password属性;
XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。
NextSiblingElement//获取下一个相邻的节点
//class information{ public: ~information(){ cout<<"~information"<<endl; } public: string title; string link; string description; string content; string lagnguage; string copyright; string pubdate; string guid; string dccreator;};void traverse(vector <information> * _vecotr){// string filename = "pagelib.dat"; std::ofstream _ostream("pagelib.dat",std::ios::app); vector<information>::iterator _begin= _vecotr->begin(); vector<information>::iterator _end= _vecotr->end(); ++_begin; while(_begin!=_end){ _ostream<<"<doc>"<<endl; _ostream<<"<title>"<<_begin->title<<"</title>"<<endl; _ostream<<"<link>"<<_begin->link<<"</link>"<<endl; _ostream<<"<description>"<<_begin->description<<"</description>"<<endl;// _ostream<<"<content>"<<_begin->content<<"</content>"<<endl; _ostream<<"</doc>"<<endl; ++_begin; }}
void example1(){ vector<information> information_vector; XMLDocument doc; doc.LoadFile("douban.book.review.xml"); cout<<doc.Error()<<endl; if(0!=doc.Error()){ cout<<"load file error"<<endl; return; }// doc.Print(); XMLElement* root = doc.RootElement(); XMLElement* channel = root->FirstChildElement("channel"); if(NULL==channel){ cout<<"get first chile element error "<<endl; return ; } XMLElement * title = channel->FirstChildElement("title"); cout<<title->GetText()<<endl; cout<<title->Name()<<endl; XMLElement * description = title->NextSiblingElement(); cout<<description->GetText()<<endl; cout<<description->Name()<<endl; XMLElement * language = description->NextSiblingElement(); cout<<language->GetText()<<endl; cout<<language->Name()<<endl; XMLElement * copyright = language->NextSiblingElement(); cout<<copyright->GetText()<<endl; cout<<copyright->Name()<<endl; XMLElement * pubDate = copyright->NextSiblingElement(); cout<<pubDate->GetText()<<endl; cout<<pubDate->Name()<<endl; XMLElement * item=pubDate->NextSiblingElement(); while(item){ information tmp; cout<<"sucess in item"<<endl; XMLElement *item_child= item->FirstChildElement(); while(item_child){ cout<<item_child->GetText()<<endl; if(strcmp(item_child->Name(),"title")==0){// tmp.title.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.title.clear(); tmp.title.append(item_child->GetText()); cout<<"title-------------------------------------------"<<endl; }else if(strcmp(item_child->Name(),"link")==0){// tmp.link.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.link.clear(); tmp.link.append(item_child->GetText()); cout<<"link-------------------------------------------"<<endl; }else if(strcmp(item_child->Name(),"description")==0){// tmp.link.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.description.clear(); tmp.description.append(item_child->GetText()); cout<<"description------------------------------------"<<endl; }else if(strncmp(item_child->Name(),"content",7)==0){// tmp.content.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.content.clear(); tmp.content.append(item_child->GetText()); cout<<"content------------------------------------"<<endl; }else if(strcmp(item_child->Name(),"dc:creator")==0){// tmp.dccreator.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.dccreator.clear(); tmp.dccreator.append(item_child->GetText()); cout<<"dc:creator ------------------------------------"<<endl; }else if(strcmp(item_child->Name(),"pubdate")==0){// tmp.pubdate.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.pubdate.clear(); tmp.pubdate.append(item_child->GetText()); cout<<"pubdate ------------------------------------"<<endl; }else if(strcmp(item_child->Name(),"guid")==0){// tmp.guid.replace(tmp.title.begin(),tmp.title.end(),item_child->GetText()); tmp.guid.clear(); tmp.guid.append(item_child->GetText()); cout<<"guid------------------------------------"<<endl; } item_child = item_child->NextSiblingElement(); } information_vector.push_back(tmp);// delete tmp; item = item->NextSiblingElement(); } traverse(&information_vector);}int main(){ example1(); return 0;}
- tinyxml2解析RSS文件 并将其转换为.dat格式
- java将xml解析,然后将其转换为json格式(①)
- java将xml解析,然后将其转换为json格式(②)
- java将xml解析,然后将其转换为json格式(1)
- 转并学习: 将rar文件转换为zip格式
- js解析xml字符串或xml文件,将其转换为xml对象方法
- 利用Jacob将 lrc 文件转换为 docx 文件并整理格式
- 读取excel并将其转换为xml
- tinyxml2解析XML文件
- IP库DAT格式转换为sql语句的格式
- android 将SQLite数据库的表格导出为csv格式,并解析csv文件
- 【Android】pull解析xml文件+将数据存储为xml格式,并保存在内存里
- 将bmp图像转换为CCS可以识读的dat文件,Matlab实现
- FlashFXP 2 FileZilla 将 FlashFXP 的站点文件 Sites.dat 或其导出的 *.ftp 站点信息文件转换为 FileZilla 的站点文件,以便导入到 FileZilla 中,实现 FTP 客户端从 FlashFXP 到
- 获取桌面并将其保存为位图文件
- 将数字格式设置为文本格式,并使其出现左上角绿色小三角
- 将matlab中数据保存为txt或dat格式
- 将matlab中数据保存为txt或dat格式
- 延迟3秒,淡出效果
- ZZL的cf#419 div2
- java——覆盖equals方法没有想象中那么简单
- make(9)
- 计算机文件读写原理
- tinyxml2解析RSS文件 并将其转换为.dat格式
- nexus-3.2.0-01.zip安装及使用说明
- Guava ListenableFuture实现异步非阻塞调用
- Javascript模块化编程
- 51nod 1577 异或凑数
- ios 绘图
- Mac系统中安装JDK
- 【机器学习小白日记】 Ubuntu16.04+Caffe+GTX1070+CUDA8.0+OpenCV3.2安装
- c++ map/multimap,set/multiset的使用(续)