博客
关于我
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/

你可能感兴趣的文章
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>