博客
关于我
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 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>