博客
关于我
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中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>