Parsing XML Elements using TinyXML

我们两清 提交于 2019-12-03 16:08:02

After a lot of playing around with the code, here is the solution! (With help from HERE)

TiXmlDocument doc("EGC_Cards.xml");
combo = (CComboBox*)GetDlgItem(IDC_EGC_CARD_TYPE);

if(doc.LoadFile())
{
    TiXmlHandle hDoc(&doc);
    TiXmlElement *pRoot, *pParm;
    pRoot = doc.FirstChildElement("EGCs");
    if(pRoot)
    {
        pParm = pRoot->FirstChildElement("card");
        int i = 0; // for sorting the entries
        while(pParm)
        {
            combo->InsertString(i, pParm->Attribute("type"));
            pParm = pParm->NextSiblingElement("card");
            i++;
        }
    }
}
else 
{
    AfxMessageBox("Could not load XML File.");
    return false;
}

there should be a Attribute method that takes and attribut name as parameter see: http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html

from the documentation I see the code would look like:

    hRoot.FirstChildElement("card").ToElement()->Attibute("type");

However for the type of thing you are doing I would use XPATH if at all possible. I have never used it but the TinyXPath project may be helpful if you choose to go that route the link is: http://tinyxpath.sourceforge.net/

Hope this helps.

The documentation I am using to help you from is found at: http://www.grinninglizard.com/tinyxmldocs/hierarchy.html

What you need is to get the attribute type from the element card. So in your code it should be something like:

const char * attribval = hRoot.FirstChild("card").ToElement()->Attribute("card");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!