MFC&XTP库读取和写入XML文件

注:XTP库文件可以在网上下载,然后引用到所写的程序中

1、XML示范文件

//示例文件
<?xml version="1.0" encoding="UTF-8"?>
<GalleryLibrary CompactMode="1">
	<GalleryItems>
		<GalleryItem ID="{704C9D93-2493-4260-9D11-7812E8EB6351}" Class="CPipeNodeNormalItem" DisplayName="动力" BlockName="动力"/>
		<GalleryItem ID="{AFCA1A08-0800-4319-8FD5-AD7BF26946D1}" Class="CPipeNodeValveGalleryItem" DisplayName="蝶阀" BlockName="蝶阀"/>
		<GalleryItem ID="{442BC09B-3C44-4ed9-81F9-5ECD89F7C8A1}" Class="CPipeNodeValveGalleryItem" DisplayName="闸阀" BlockName="闸阀"/>
		<GalleryItem ID="{30C040C0-55A6-4609-A6C4-F2DCC6FEF21C}" Class="CPipeNodeValveGalleryItem" DisplayName="球阀" BlockName="球阀"/>
	</GalleryItems>
</GalleryLibrary>

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<stuNodeData>& 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<stuNodeData>& 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);
	
	//pPX->IsLoading()此处可以通过CXTPPropExchangeXMLNode 
	//px(FALSE, NULL, _T("GalleryLibrary"))的第一个参数来定
	
	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
	{
		// 添加 SizeList 节点
		CXTPPropExchangeSection secList(pPX->GetSection(_T("DrainageList")));
		CXTPPropExchangeEnumeratorPtr enumData(pPX->GetEnumerator(_T("Drainage")));
		POSITION pos = enumData->GetPosition();

		for (int j = 0; j < arrDrainageData.GetSize(); j++)
		{
			// 添加 Size 节点
			CXTPPropExchangeSection sec(enumData->GetNext(pos));

			arrDrainageData[j]->DoPropExchange(&sec);
			((CXTPPropExchangeXMLNode*)&secList)->PutSection((CXTPPropExchangeXMLNode*)&sec);
		}
	}
}
  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧特克_Glodon

很高兴能帮助到您!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值