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 ...
随机推荐
- react native 问题点
问题点一:安装了react-native-vector-icons后,编译出错 版本: "react": "16.2.0", "react-nativ ...
- HDU 6462.人类史上最大最好的希望事件-递推 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)
人类史上最大最好的希望事件 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- gradle打包分编译环境
gradle打包分测试.开发.生产环境 buildTypes { debug { signingConfig signingConfigs.myConfig buildConfigField(&quo ...
- CodeForces 738D Sea Battle
抽屉原理. 先统计最多有$sum$个船可以放,假设打了$sum-a$枪都没打中$a$个船中的任意一个,那么再打$1$枪必中. #pragma comment(linker, "/STACK: ...
- 洛谷P2874 [USACO07FEB]新牛棚Building A New Barn [贪心]
题目传送门 题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wan ...
- python 写文件write(string), writelines(list) ,读文件
read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...
- POJ1704 Georgia and Bob 博弈论 尼姆博弈 阶梯博弈
http://poj.org/problem?id=1704 我并不知道阶梯博弈是什么玩意儿,但是这道题的所有题解博客都写了这个标签,所以我也写了,百度了一下,大概是一种和这道题类似的能转换为尼姆博弈 ...
- python基础之闭包函数与装饰器
闭包函数: 什么是闭包函数: 闭指的是定义在一个函数内部 包指的是该函数包含对外部作用域(非全局作用域)名字的引用 def counter(): n=0 def incr(): nonlocal n ...
- 都9102年了,还不会Docker?10分钟带你从入门操作到实战上手
Docker简述 Docker是一种OS虚拟化技术,是一个开源的应用容器引擎.它可以让开发者将应用打包到一个可移植的容器中,并且该容器可以运行在几乎所有linux系统中(Windows10目前也原生支 ...
- Do waiting or suspended tasks tie up a worker thread?
https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...