Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyXML getting Value

given XML like:

<a>
    <result>0</result>
    <data>I9C3J9N3cCTZdKGK+itJW1Q==</data>
</a>

I need to get the fact that <result> is 0 and act upon it.

I am doing:

TiXmlDocument doc;
bool bOK = doc.Parse((const char*)chunk.memory, 0, TIXML_ENCODING_UTF8);
if (bOK)
{
    TiXmlHandle hDoc(&doc);
    TiXmlElement *pRoot, *pParm, *pParm2;
    pRoot = doc.FirstChildElement("a");
    if(pRoot)
    {
        pParm = pRoot->FirstChildElement("result");
        if (pParm)
        {
            if (pParm->GetText()=="0")
            {
                pParm2 = pRoot->NextSiblingElement("data");
                if (pParm2)
                {
                    sValue = pParm2->GetText();
                    std::cout << "sValue: " << sValue << std::endl;
                }
            }
        }
    }
}

I thought that GetText() was the right answer, but I am doing something wrong because I never get inside the if to check the <data> element.

Can anyone shed some light for me?

like image 577
Jasmine Avatar asked Nov 23 '25 01:11

Jasmine


1 Answers

Because in your case, <data> isn't Sibling of <a>.

You're checking pRoot->NextSiblingElement("data") while you should check for pParm->NextSiblingElement("data");

You could also change it to

pParm2 = pRoot->FirstChildElement("data");

Edit: Sorry, i thought you were referring to this if:

if (pParm2)

So, the solution could be this:

if (std::string(pParm->GetText())=="0")

or

if (strcmp(pParm->GetText(), "0"))

You choose. I prefer the first one.

Edit 2:

I'm really sorry, I was forgetting that strcmp return the first position of where the 2 strings are the same, so, in your case it should be:

if (strcmp(pParm->GetText(), "0") == 0)

You need to include <string.h> too.

like image 72
Jamby Avatar answered Nov 24 '25 16:11

Jamby