Write

#include <iostream>
#include <string>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
/*
xml
Documnet
Declaration---Node
Element---Node
Comment;    //注释  ---NodeAttribute;  //属性
Text;       //内容
//层级收
rapidxml所表示的类型都为类模板
*/
static const int buf_len = 1024;
static char buf[buf_len] = { 0 };void WriteXml(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);// 注释节点1rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");root->append_node(comment1);// 普通节点1rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");for (int i = 0; i < 10; ++i){rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student");sprintf_s(buf, "student_%02d", i);//format// 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(100 - i).c_str())));students->append_node(one_student);}root->append_node(students);// 注释节点2rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");root->append_node(comment2);// 普通节点2rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books");for (int i = 0; i < 10; ++i){rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::node_element, "book");sprintf_s(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(50 - i).c_str())));books->append_node(one_book);}root->append_node(books);char *end = rapidxml::print(buf, doc, 0);//将xml文档写入buff*end = 0;//加结束符std::cout << buf;std::ofstream outfile(file_name, std::ios::out);if (outfile){outfile << buf;outfile.close();}}int main()
{WriteXml("info.xml");return 0;
}

Read

#include <iostream>
#include <fstream>
#include "rapidxml.hpp"static const int buf_len = 1024;
static char buf[buf_len] = { 0 };
void ReadXml(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 documentdoc.parse<0>(buf);//解析获取xml文档// 从xml中取得根节点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()
{ReadXml("info.xml");return 0;
}

rapidxml的简单使用相关推荐

  1. 使用 rapidxml 做配置文件

    对于配置文件,一般会选用ini,xml 等等的配置格式.如何快速高效的从文件内读取自己想要的信息是每个做配置文件想要达到的效果.对以小型开发我们并不用时用到msxml这种重量级的解析器.那样会给自己添 ...

  2. rapidxml对unicode的支持

    为了提高duilib创建布局控件的效率,在LuaDui项目中使用rapidxml解析器替换了duilib库自带的xml解析器. duilib使用unicode编译,所以rapidxml需要解析unic ...

  3. 使用rapidxml解析,使用folly dynamic序列化输出json

    使用rapidxml解析,使用folly dynamic序列化输出json Folly是Facebook的一个重型库, rapidxml是一个轻量级的效率极高的xml解析器 目的: 将如下的xml中部 ...

  4. 服务器开发24补充:市面上一些xml第三方库解析速度的对比,tinyxml、tinyxml2、Qt、RapidXml、PugiXml,和tinyxml与tinyxml2的对比

    文章目录 零.背景前提 一.Qt - QDomDocument 1)测试代码 2)测试结果 二.TinyXml(略) 三.TinyXml2 1)测试代码 2)运行效果及结论 四.RapidXml 1) ...

  5. Tinyxml简单使用法

    1.概要介绍一下: 尝试了tinyxml,rapidxml,libxml2等3中第3方库,前两者相对较为轻量级,libxml2非常完善,强大. 这是网上总结出来的优缺点: 1.tinyxml当节点内容 ...

  6. 在docker上安装部署tomcat项目 超简单,拿来主义

    在docker中部署tomcat,非常简单,而且省去了手动安装jdk等步骤,只需要将war包复制在容器tomcat实例中的webapps下面即可.以下将详细讲解流程: 在windows中打好包以后用w ...

  7. Linux下tomcat的安装与卸载以及配置(超简单)

    无敌简单的几步 1.安装 //首先你需要下载好tomcat包 sudo tar -xvzf apache-tomcat-7.0.85.tar.gz(这里是包名) -C 你要放的位置 2.卸载 rm - ...

  8. Docker安装Apache与运行简单的web服务——httpd helloworld

    Docker运行简单的web服务--httpd helloworld目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker运行简单的web ...

  9. Docker的安装、镜像源更换与简单应用

    Docker的安装.镜像源更换与简单应用[阅读时间:约20分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.Docker的安装 四.Docker的简单应用 1. ...

  10. 基于Golang的简单web服务程序开发——CloudGo

    基于Golang的简单web服务程序开发--CloudGo[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 (1)基本要求 (2)扩展要求 三.具体 ...

最新文章

  1. android 相片裁剪空间,科学网—根据需要的空间范围对目标图层进行空间裁剪 - 孙露的博文...
  2. Django用来作为爬虫框架浅谈
  3. 判断字符串 正则_(重学前端 - JavaScript(模块一)) 14、引用类型之 RegExp (正则)(详述)...
  4. 探索MySql.Data.dll
  5. [云炬创业基础笔记] 第四章测试4
  6. 编译高性能linux so库,关于如何在linux环境下生成a库和so库(改,附图)-Go语言中文社区...
  7. java动态调用c++库
  8. findbugs插件_Intellij静态代码扫描插件SpotBugs
  9. JavaScript设计模式(二)-- 工厂方法模式
  10. pytorch torch.nn.Module.register_buffer
  11. git全局配置用户名和密码_git---全局设置用户名、密码、邮箱
  12. Atitit  代理与分销系统(1)  子代理 充值总额功能设计概览 sum() groubpy subagt
  13. MediaWiki中的各种功能和设置
  14. PCB电源 - 开关电源介绍、工作原理、电路图
  15. NC 一些常用方法总结
  16. seleinum+requets 下载歌曲
  17. 湖南省中职计算机应用教资笔试+面试心得
  18. 前端工程化----Node.js基础篇
  19. java计算机毕业设计旅游景点推荐系统源码+系统+mysql数据库+lw文档
  20. studioone机架效果模板_贾爽的分享-贾爽:带你认识StudioOne机架自带的两个混响效果器!...

热门文章

  1. 海量数据排序问题一一 100G 数据,只有 100M 内存,怎么排序?
  2. 2023上海大学电气工程及其自动化考研必看上岸经验指导
  3. 好物分享 Kvaser支持CANFD功能的CAN卡都有哪些?
  4. pdf复制json不全的问题
  5. Jmeter的面试题集
  6. 蒙特卡洛模拟分析市场风险
  7. FGSM对抗样本trick汇总
  8. 对抗攻击FGSM的纯粹版FGNS
  9. 宏基因组/微生物组杂志简介及2020最新影响因子
  10. 深度 ghost linux系统,用Ghost实现Linux系统的备份