博客
关于我
C++读取和写入XML文件
阅读量:211 次
发布时间:2019-02-28

本文共 3548 字,大约阅读时间需要 11 分钟。

1、XML示范文件

以下是一个XML示范文件的示例,展示了如何结构化和存储数据:

2、数据结构

以下是与数据结构相关的定义:

struct stuNodeData{      CString strGuid;      CString strClassName;      CString strDisplayName;      CString strBlockName;      stuNodeData()      {          strGuid = _T "";          strClassName = _T "";          strDisplayName = _T "";          strBlockName = _T "";      }      stuNodeData(const stuNodeData& src)      {          strGuid = src.strGuid;          strClassName = src.strClassName;          strDisplayName = src.strDisplayName;          strBlockName = src.strBlockName;      }      void DoPropExchange(CXTPPropExchange* pPX)      {          PX_String(pPX, _T("ID"), strGuid);          PX_String(pPX, _T("Class"), strClassName);          PX_String(pPX, _T("DisplayName"), strDisplayName);          PX_String(pPX, _T("BlockName"), strBlockName);      }  };

3、读取函数

以下是读取数据文件的函数实现:

void LoadDatFile(CString dataPath, std::vector
& vecNodeData){ CXTPPropExchangeXMLNode px(TRUE, NULL, _T("GalleryLibrary")); if (!px.LoadFromFile(dataPath)) return; if (!px.OnBeforeExchange()) return; px.SetCompactMode(TRUE); CString strPattern = _T("GalleryItems/GalleryItem"); CXTPPropExchangeEnumeratorPtr enumData(px.GetEnumerator(strPattern)); POSITION pos = enumData->GetPosition(); while (pos) { CXTPPropExchangeSection sec(enumData->GetNext(pos)); stuNodeData data; data.DoPropExchange(&sec); vecNodeData.push_back(data); } }

4、写入函数

以下是写入数据文件的函数实现:

bool WriteDatFile(CString& strFilePath, std::vector
& vecNodeData){ CXTPPropExchangeXMLNode px(FALSE, NULL, _T("GalleryLibrary")); if (!px.OnBeforeExchange()) { return false; } px.SetCompactMode(TRUE); if (vecNodeData.size() <= 0) { return false; } CXTPPropExchangeSection secStandardList(px.GetSection(_T("StandardList"))); CXTPPropExchangeEnumeratorPtr enumData(secStandardList->GetEnumerator(_T("Standard"))); POSITION pos = enumData->GetPosition(); for (int i = 0; i < vecNodeData.GetCount(); i++) { CXTPPropExchangeSection sec(enumData->GetNext(pos)); vecNodeData.at(i)->DoPropExchange(&sec); } px.SaveToFile(strFilePath); return true; }

5、更高层级的扩展示范例子

以下是一个更高层级的扩展示例:

void CustomWellData::DoPropExchange(CXTPPropExchange* pPX){      PX_String(pPX, _T("name"), strStandardNm);      PX_String(pPX, _T("material"), strMaterial);      if (pPX->IsLoading())      {          CXTPPropExchangeEnumeratorPtr enumData(pPX->GetEnumerator(_T("DrainageList/Drainage")));          POSITION pos = enumData->GetPosition();          while (pos)          {              CXTPPropExchangeSection sec(enumData->GetNext(pos));              CustomDrainageData* pData = new CustomDrainageData();              pData->DoPropExchange(&sec);              arrDrainageData.Add(pData);          }      }      else      {          CXTPPropExchangeSection secList(pPX->GetSection(_T("DrainageList")));          CXTPPropExchangeEnumeratorPtr enumData(pPX->GetEnumerator(_T("Drainage")));          POSITION pos = enumData->GetPosition();          for (int j = 0; j < arrDrainageData.GetSize(); j++)          {              CXTPPropExchangeSection sec(enumData->GetNext(pos));              arrDrainageData[j]->DoPropExchange(&sec);              ((CXTPPropExchangeXMLNode*)&secList)->PutSection((CXTPPropExchangeXMLNode*)&sec);          }      }  }

转载地址:http://udqn.baihongyu.com/

你可能感兴趣的文章
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>