rapidxml的常见读写操作
rapidxml官网地址:http://rapidxml.sourceforge.net/
rapidxml只包含4个hpp头文件,把这四个头文件放到项目中,即可使用rapidxml
#include <iostream>
#include <string>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp" static const int buf_len = ;
static char buf[buf_len] = { }; void create(const char * file_name)
{
rapidxml::xml_document<> doc;
// 声明
rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
declaration->append_attribute(doc.allocate_attribute("version", "1.0"));
declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(declaration); // 根节点
rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
doc.append_node(root); // 注释节点1
rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, , "all students info");
root->append_node(comment1); // 普通节点1
rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");
for (int i = ; i < ; ++i)
{
rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student");
sprintf(buf, "student_%02d", i);
// doc.allocate_string 的作用是将源字符串深拷贝一份
one_student->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));
one_student->append_attribute(doc.allocate_attribute("score", doc.allocate_string(std::to_string( - i).c_str())));
students->append_node(one_student);
}
root->append_node(students); // 注释节点2
rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, , "all books info");
root->append_node(comment2);
// 普通节点2
rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books");
for (int i = ; i < ; ++i)
{
rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::node_element, "book");
sprintf(buf, "book_%02d", i);
// doc.allocate_string 的作用是将源字符串深拷贝一份
one_book->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));
one_book->append_attribute(doc.allocate_attribute("price", doc.allocate_string(std::to_string( - i).c_str())));
books->append_node(one_book);
}
root->append_node(books); std::ofstream outfile(file_name, std::ios::out);
if (outfile)
{
char *end = rapidxml::print(buf, doc, );
*end = ;
outfile << buf;
outfile.close();
}
} void parse(const char * file_name)
{
std::ifstream infile(file_name, std::ios::in);
if (!infile)
{
return;
}
infile.read(buf, buf_len);
std::cout << buf << std::endl; rapidxml::xml_document<> doc;
doc.parse<>(buf);
// 取得根节点
rapidxml::xml_node<> *root = doc.first_node("root");
// 遍历students的子节点
for (rapidxml::xml_node<> * node = root->first_node("students")->first_node(); node; node = node->next_sibling())
{
std::cout << node->first_attribute("name")->value() << ", "
<< node->first_attribute("score")->value() << std::endl;
}
// 遍历books的子节点
for (rapidxml::xml_node<> * node = root->first_node("books")->first_node(); node; node = node->next_sibling())
{
std::cout << node->first_attribute("name")->value() << ", "
<< node->first_attribute("price")->value() << std::endl;
}
} int main()
{
const char * file_name = "info.xml";
create(file_name);
parse(file_name);
return ;
}
生成的示例文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--all students info-->
<students>
<student name="student_00" score="100"/>
<student name="student_01" score="99"/>
<student name="student_02" score="98"/>
<student name="student_03" score="97"/>
<student name="student_04" score="96"/>
<student name="student_05" score="95"/>
<student name="student_06" score="94"/>
<student name="student_07" score="93"/>
<student name="student_08" score="92"/>
<student name="student_09" score="91"/>
</students>
<!--all books info-->
<books>
<book name="book_00" price="50"/>
<book name="book_01" price="49"/>
<book name="book_02" price="48"/>
<book name="book_03" price="47"/>
<book name="book_04" price="46"/>
<book name="book_05" price="45"/>
<book name="book_06" price="44"/>
<book name="book_07" price="43"/>
<book name="book_08" price="42"/>
<book name="book_09" price="41"/>
</books>
</root>
rapidxml的常见读写操作的更多相关文章
- pugixml 的常见读写操作
pugixml github地址 : https://github.com/zeux/pugixml pugixml 可以在github上直接下载到源码,包括两个头文件(pugixml.hpp pu ...
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
ASP.NET MVC Filters 4种默认过滤器的使用[附示例] 过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响 ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- java封装实现Excel建表读写操作
对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...
- 一个I/O线程可以并发处理N个客户端连接和读写操作 I/O复用模型 基于Buf操作NIO可以读取任意位置的数据 Channel中读取数据到Buffer中或将数据 Buffer 中写入到 Channel 事件驱动消息通知观察者模式
Tomcat那些事儿 https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==&mid=2650860016&idx=2&sn=549 ...
- [ Python ] 文件的读写操作
1. 文件读写操作 读写文件是最常见的 IO 操作, Python 内置了读写文件的函数.在磁盘上读写文件的功能是由操作系统提供的,所以读写文件是请求操作系统打开一个文件对象(通常称为文件描述符),然 ...
- [转]Android - 文件读写操作 总结
转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...
- App.Config详解及读写操作
App.Config详解及读写操作 App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...
- 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)
算是一个小技巧吧,只是进行一些简单的读写操作.让人不爽的是xlrd和xlwt是相对独立的,两个模块的对象不能通用,读写无法连贯操作,只能单独读.单独写,尚不知道如何解决. #①xlrd(读) #cod ...
随机推荐
- Codeforces 1131 A. Sea Battle-暴力 (Codeforces Round #541 (Div. 2))
A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- python 自定义过滤器
文件目录结构: 新建文件并且命名为“templatetags” , 然后复制 __init__.py文件,拷贝到templatetags文件夹里, __pycache__文件夹可以忽略哈,那是程序运行 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- Linux命令之head
head [选项] [文件] head命令输出文件开头部分,默认情况下显示文件的头10行.如果指定多个文件,每个文件前都有一个标题,给出文件名.如果没有指定文件,或当文件为-时,读取标准输入. (1) ...
- 【hihoCoder 第133周】【hihoCoder 1467】2-SAT·hihoCoder音乐节
http://hihocoder.com/problemset/problem/1467 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #include ...
- luogu P1378 油滴扩展
题目描述 在一个长方形框子里,最多有N(0≤N≤6)个相异的点,在其中任何一个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放置下一个油滴. ...
- 【高斯消元】CDOJ1785 曜酱的线性代数课堂(三)
高斯消元求行列式板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...
- 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression
模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...
- 2016.4.3NOI上较难的动规题目(仔细分析样例)--王老师讲课整理
1.NOI 191:钉子和小球 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个三角形木板,竖直立放,上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每 ...
- Educational Codeforces Round 9 E. Thief in a Shop dp fft
E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...