把如下图几个文件放到工程目录(hpp文件)

新建工程进行读写测试,代码如下:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"  //rapidxml::file
#include "rapidxml_print.hpp"  //rapidxml::print
#include <windows.h>
#include <iostream>

void writeFile(const char * file_name)
{
    char buf[1024] = { 0 };
    rapidxml::xml_document<> doc;
    // XML头的声明
    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);
    rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");
    root->append_node(comment1);
    rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");
    for (int i = 0; i < 10; ++i)
    {
        rapidxml::xml_node<>* n1 = doc.allocate_node(rapidxml::node_element, "student");
        // doc.allocate_string 的作用是将源字符串深拷贝一份
        n1->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));
        n1->append_attribute(doc.allocate_attribute("score", doc.allocate_string(std::to_string(100 - i).c_str())));
        students->append_node(n1);
    }
    root->append_node(students);
    std::ofstream outfile(file_name, std::ios::out);
    if (outfile)
    {
        std::string text;
        rapidxml::print(std::back_inserter(text), doc, 0);
        outfile << text;
        outfile.close();
    }
}

void readFile(const char * fileName)
{
    std::ifstream inf(fileName, std::ios::in);
    if (!inf)
    {
        return;
    }

inf.seekg(0, std::ios::end);
    int nLen = inf.tellg();
    inf.seekg(0, std::ios::beg);
    char * strc = new char[nLen+1];
    ZeroMemory(strc, nLen + 1);
    inf.read(strc, nLen);
    rapidxml::xml_document<> doc;
    doc.parse<0>(strc);
    rapidxml::xml_node<> *root = doc.first_node("root");
    int nSize = 0;
    
    rapidxml::xml_node<>* child = root->first_node("students")->first_node();
    while (child)
    {
            //判断属性是否存在
        rapidxml::xml_attribute<>* nameAttr = child->first_attribute("name");
        rapidxml::xml_attribute<>* scoreAttr = child->first_attribute("score");
        char *nameC;
        char *scoreC;
        if (nameAttr)
        {
            nameC = nameAttr->value();
        }
        if (scoreAttr)
        {
            scoreC = scoreAttr->value();
        }
        child = child->next_sibling();
    }

/*for (rapidxml::xml_node<>* child = root->first_node("students")->first_node(); child; child = child->next_sibling())
    {
        char *nameC = child->first_attribute("name")->value();
        char * homea = child->first_attribute("score")->value();
    }*/

delete strc;
    strc = NULL;
    
}

int _tmain(int argc, _TCHAR* argv[])
{
    writeFile("11.xml");
    readFile("11.xml");
    return 0;
}

RapidXML的读写相关推荐

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

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

  2. etcd 笔记(07)— 键值对读写操作过程

    1. 读写总体概述 etcd 各个模块交互的总览,如下图所示: 总体上的请求流程从上至下依次为客户端 → API 接口层 → etcd Server → etcd raft 算法库. 读请求 客户端通 ...

  3. Go 知识点(08) — 对未初始化的 channel 进行读写操作

    1. 对未初始化的 channel 进行写操作 先看下面代码 func main() {var ch chan int // 只声明,并没有初始化fmt.Printf("ch is %v\n ...

  4. Go 知识点(07)— 对已经关闭通道进行读写

    今天我们来看下对已经关闭通道进行读写会发生什么情况. 1. 对已关闭通道进行写操作 看下面代码会输出什么结果? func main() {ch := make(chan string, 1)close ...

  5. Python+OpenCV 图像处理系列(4)—— 图像像素的读写、算术运算、逻辑运算及像素的统计

    1. 像素的读写 可以根据像素的行和列的坐标获取它的像素值.对 BGR 图像而言,返回值为 B,G,R 的值. img.shape 可以获取图像的形状.它的返回值是一个包含行数 h,列数 w,通道数 ...

  6. python对文件的读写正确操作方式

    1.with open函数打开文件的各种方式 模式 意义 r 只读模式打开文件,读文件内容的指针会放在文件的开头. 操作的文件必须存在. rb 以二进制格式.采用只读模式打开文件,读文件内容的指针位于 ...

  7. 利用pandas读写HDF5文件

    一.简介 HDF5(Hierarchical Data Formal)是用于存储大规模数值数据的较为理想的存储格式,文件后缀名为h5,存储读取速度非常快,且可在文件内部按照明确的层次存储数据,同一个H ...

  8. OpenCV读写视频文件解析(二)

    OpenCV读写视频文件解析(二) VideoCapture::set 设置视频捕获中的属性. C++: bool VideoCapture::set(int propId, double value ...

  9. OpenCV读写图像文件解析

    OpenCV读写图像文件解析 imdecode 从内存中的缓冲区读取图像. C++: Mat imdecode(InputArray buf, int flags) C++: Mat imdecode ...

  10. OpenCV读写视频文件解析

    OpenCV读写视频文件解析 一.视频读写类 视频处理的是运动图像,而不是静止图像.视频资源可以是一个专用摄像机.网络摄像头.视频文件或图像文件序列. 在OpenCV 中,VideoCapture 类 ...

最新文章

  1. idea java编译报错_intellij-idea,java_idea 编译报错,intellij-idea,java - phpStudy
  2. 算法-------反转字符串
  3. 《Total Commander:万能文件管理器》——第2.3节.下载与安装
  4. 使用 Termux 查看连接到手机热点的ip地址
  5. 深度学习(25)随机梯度下降三: 激活函数的梯度
  6. maven 结合idea入门
  7. 创业型软件公司的心得
  8. C语言,利用条件语句判断是否为三角形并输出面积
  9. 杨廷琨Oracle Code大会分享:如何编写高效SQL(含PPT)
  10. mybatis_SQL映射(2)
  11. JavaScript实现对象的深度克隆及typeof和instanceof【简洁】【分享】
  12. springboot 集成 freemarker
  13. Atitit 机器学习算法分类 五大分类v5 t56.docx Atitit 机器学习算法分类 目录 1. 传统的机器学习算法 vs 深度学习 1 1.1. 传统的机器学习算法包括决策树、聚类、贝
  14. MAVEN处理本地jar包 ojdbc7 的spingboot配置
  15. 自动量程万用表的实现原理_自动量程万用表模块设计方案[图]
  16. java课设超市收银系统_超市收银系统java课程设计.doc
  17. 机器学习原理与实践(开源图书)-总目录
  18. 前端——“一看就会的”菜鸟教程网站首页制作!
  19. HTML 前后端分离,再谈前后端分离开发和部署
  20. 在VMware 16.2.2中安装Windows7

热门文章

  1. 交换机芯片技术知多少
  2. Java实体映射工具:MapStruct
  3. 【嵌入式算法】CRC校验算法
  4. 基于comsol软件的三维单模光纤模拟
  5. BeyondCompare 源代码比对解决方案
  6. mac教程:磁力种子qBittorrent 使用教程
  7. 数据可视化 基于TMDB数据集的电影数据分析(项目源码 + 数据集 + 课程设计说明书 + 可视化图表+ 运行说明等)
  8. Vue使用js读取Excel数据
  9. 密码学系列 - 双线性对
  10. 【初识SciPy库】