如何遍历具有相同名称的xml标记列表?(msxml)

时间:2015-08-02 03:11:27

标签: c++ xml

我编写了一个xml阅读器,用于从EVENT标签中获取值,但是我需要遍历列表,以便下一个EVENT标记不会覆盖这些信息。我想知道是否有人可以给我一些如何做到的提示。

当前代码:

#include "stdafx.h"
#include "MEventInfo.h"

#define MTAG_EVENTINFO  "EVENTINFO"
#define MTAG_EVENT      "EVENT"
#define MTAG_EVENT_NAME "name"
#define MTAG_EVENT_SECONDARYNAME "secondaryname"
#define MTAG_EVENT_TERTIARYNAME "tertiaryname"
#define MTAG_EVENT_QUARTERARYNAME "quarteraryname"
#define MTAG_EVENT_OBJECTIVE  "objective"
#define MTAG_EVENT_DESC  "description"
#define MTAG_EVENT_OBJCOLOR "objcolor"
#define MTAG_EVENT_DESCOLOR "descolor"

MEvents::MEvents()
{

}

MEvents::~MEvents()
{

}

MEvents* MEvents::GetInstance()
{
    static MEvents m_EventMgr;
    return &m_EventMgr;
}

bool MEvents::ReadXml(const char* szFileName)
{
    MXmlDocument    xmlIniData;

    xmlIniData.Create();

    if (!xmlIniData.LoadFromFile(szFileName))
    {
        xmlIniData.Destroy();
        return false;
    }

    MXmlElement rootElement, chrElement, attrElement;
    char szTagName[256];

    rootElement = xmlIniData.GetDocumentElement();

    int iCount = rootElement.GetChildNodeCount();

    for (int i = 0; i < iCount; i++)
    {
        chrElement = rootElement.GetChildNode(i);
        chrElement.GetTagName(szTagName);
        if (szTagName[0] == '#') continue;

        if (!stricmp(szTagName, MTAG_EVENTINFO))
        {
            LoadEventInfo(chrElement); //this is so I can load <EVENT inside of <EVENTINFO></EVENTINFO>
        }
    }

    xmlIniData.Destroy();
    return true;
}

void MEvents::LoadEventInfo(MXmlElement& element)
{
    char szAttrValue[256];
    char szAttrName[64];
    char szTagName[128];


    int nChildCount = element.GetChildNodeCount();
    MXmlElement chrElement;
    for (int i = 0; i < nChildCount; i++)
    {
        chrElement = element.GetChildNode(i);
        chrElement.GetTagName(szTagName);
        if (szTagName[0] == '#') continue;

        if (!stricmp(szTagName, MTAG_EVENT))
        { 
            int nAttrCount = chrElement.GetAttributeCount();
            for (int j = 0; j < nAttrCount; ++j)
            {


                chrElement.GetAttribute(j, szAttrName, szAttrValue);
                if (!stricmp(szAttrName, MTAG_EVENT_NAME))
                {
                    strEventName = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_SECONDARYNAME))
                {
                    strSecondaryName = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_TERTIARYNAME))
                {
                    strTertiaryName = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_QUARTERARYNAME))
                {
                    strQuarteraryName = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_OBJECTIVE))
                {
                    strEventObjective = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_DESC))
                {
                    strEventDesc = szAttrValue;
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_OBJCOLOR))
                {
                    nObjColor = atoi(szAttrValue);
                }
                else if (!stricmp(szAttrName, MTAG_EVENT_DESCOLOR))
                {
                    nDescColor = atoi(szAttrValue);
                }
                Log(3, "%s - %s\n", szAttrName, szAttrValue); // 3 = send information both to a txt and to the application.

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

线程可以关闭,解决了问题。它已经迭代而没有覆盖最后一个标记,我只是没有正确记录它。我还更进了一步,通过使用向量并找到它,然后将其发送到客户端,如果在xml中匹配了可能的房间名称的任何组合,则将其作为纯服务器端功能。