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

你可能感兴趣的文章
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新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>