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 ...
随机推荐
- Java中面向对象的分拣存储
Student.java package yzhou.map; /** * 学生类 * * * @author 洋 * */ public class Student { private String ...
- HRBUST 1311 火影忍者之~忍者村
求连通块. $ABC$之间建好边,然后计算连通块的个数. #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...
- 洛谷——P3414 SAC#1 - 组合数
P3414 SAC#1 - 组合数 题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:http://191.101.11.174/mgzd . 题目描述 辣 ...
- Flask实战第60天:帖子分页技术实现
编辑manage.py,添加测试帖子 @manager.command def create_test_post(): for x in range(1, 100): title = '标题{}'.f ...
- 主数据及其管理MDM
什么是主数据 企业数据的管理包含主数据,元数据,交易数据. 主数据是描述企业核心实体的基础数据,比如客户.用户.产品.员工等. 它是具有高业务价值的.可以在企业内跨越各个业务部门被重复使用的数据,并且 ...
- CSS 笔记——定位尺寸
3. 定位尺寸 -> 尺寸 (1)height 基本语法 height : auto | length 语法取值 auto : 默认值.无特殊定位,根据HTML定位规则分配 length : 由 ...
- hdu 5961 传递 (2016ccpc 合肥站 A题)
传递 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- [CF985G]Team Players
题意:给出一个图,求$\sum\limits_{\substack{i\lt j\lt k\\\nexists(i,j),(j,k),(i,k)}}Ai+Bj+Ck$ 挺好的一道题==,就是稍微毒了点 ...
- 【计算几何】【bitset】Gym - 101412G - Let There Be Light
三维空间中有一些(<=2000)气球,一些光源(<=15),给定一个目标点,问你在移除不超过K个气球的前提下,目标点所能接受到的最大光照. 枚举每个光源,预处理其若要照射到光源,需要移走哪 ...
- 【dfs序+AC自动机+树状数组】BZOJ2434-[Noi2011]阿狸的打字机
[题目大意] 输入一个字符串,其中:(1)a..z:在字符串末尾添加当前字符(2)P:输出当前字符串(3)B:从当前字符串末尾删去一个字符. 给出m组查询,输出第i个输出的字符串在第j个输出的字符串内 ...