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的常见读写操作的更多相关文章

  1. pugixml 的常见读写操作

    pugixml github地址 : https://github.com/zeux/pugixml pugixml 可以在github上直接下载到源码,包括两个头文件(pugixml.hpp  pu ...

  2. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开

    ASP.NET MVC Filters 4种默认过滤器的使用[附示例]   过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响 ...

  3. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  4. java封装实现Excel建表读写操作

    对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...

  5. 一个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 ...

  6. [ Python ] 文件的读写操作

    1. 文件读写操作 读写文件是最常见的 IO 操作, Python 内置了读写文件的函数.在磁盘上读写文件的功能是由操作系统提供的,所以读写文件是请求操作系统打开一个文件对象(通常称为文件描述符),然 ...

  7. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  8. App.Config详解及读写操作

    App.Config详解及读写操作   App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...

  9. 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)

    算是一个小技巧吧,只是进行一些简单的读写操作.让人不爽的是xlrd和xlwt是相对独立的,两个模块的对象不能通用,读写无法连贯操作,只能单独读.单独写,尚不知道如何解决. #①xlrd(读) #cod ...

随机推荐

  1. Ubuntu 18.04安装网易云音乐(转载)

    作为Ubuntu下唯一一款超级好用的音乐软件,必须下载. 提升为root权限后操作 0 : 网易云音乐1.0.0(该版本较为好安装)下载地址 http://s1.music.126.net/downl ...

  2. 345. Reverse Vowels of a String【Easy】【双指针-反转字符串中的元音字符】

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  3. HDU 5861 Road(线段树 区间修改 单点查询)

    Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. Python开发基础-Day5-字符编码、文件处理和函数基础(草稿)

    字符编码 为什么要有字符编码? 字符编码是为了让计算机能识别我们人写的字符,因为计算机只认识高低电平,也就是二进制数"0","1". 一个文件用什么编码方式存储 ...

  5. Java多线程-run方法与start方法的区别

    package com.interview; /** * java多线程的两种实现方式以及run.start方法的区别 * @author MEI.LIU * */ public class Thre ...

  6. jquery checkbox用法汇总

    来源:http://www.jb51.net/article/75717.htm 1.全选 ? 1 2 3 $("#btn1").click(function(){ $(" ...

  7. 推荐系统中的SVD

    本文主要参考:Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model 在用户对自己需求相对 ...

  8. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分

    D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...

  9. JS零基础一步一步做应用全记录

    1.起因 作为几个外卖重度依赖癌晚期患者,呆宿舍的时候几个人一起叫外卖已经是常事.偶然看到隔壁宿舍在饿了么订餐的时候,看到在饿了么的首页上有一个谁去拿外卖的一个小游戏/工具,感觉这个小细节,饿了么把握 ...

  10. hdu1428漫步校园

    #include <queue> #include <iostream> #include <algorithm> #include <cstring> ...