samedi 1 août 2015

How to iterate through a list of xml tags that have the same name?(msxml)

I've written an xml reader that grabs the values out of the tag EVENT, however I need to iterate through the list so the information doens't get overwritten by the next EVENT tag. I was wondering if someone could give me some tips on how to do that.

Current code:

#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.

            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire