C++读取和写入XML文件

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

1、XML示范文件

//示例文件

<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& 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);

    //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);
        }
    }
}

你可能感兴趣的:(C++)