简体   繁体   中英

using TinyXML Parsing XML Elements - Infinite loop

I want to read an xml file where several tests are found, but I always get the first one and it does not come out of the loop. If I do an iteration well, but when I do the bubcle, I can not move on to the next test.

And if I use // pBodys = pRoot-> NextSiblingElement ("Test"); I miss an error in the 2 iteration, ( https://i.gyazo.com/9a108bf422299b66abfe91127668a63c.png ) if I do not use it it stays in an infinite loop

( https://i.gyazo.com/133be25514a8a000fce87e2fc7cc52ad.png )

I can not move on. Sorry for the google translation. a greeting

    int main()
    {
        XMLDocument doc;
        doc.LoadFile("example.xml");
        XMLElement *pRoot, *pBodys, *pParms, *pParms2, *pProcess, *pApp, *pLineFormat, *pParm, *pParm2, *count;
        pRoot = doc.FirstChildElement("Tests");
        if (pRoot)
        {
            count = pRoot->FirstChildElement("count");
            std::cout << "cont=" << count->Attribute("cont") << std::endl;
            pBodys = pRoot->FirstChildElement("Test");
            //for (int i = 0; i < (int)count->Attribute("cont"); i++) {


            std::cout << "id=" << pBodys->Attribute("id") << std::endl;
            if (pBodys) {
                pParms = pBodys->FirstChildElement("Inputs");
                if (pParms)
                {
                    pParm = pParms->FirstChildElement("Input");
                    while (pParm) {

                        std::cout << "port=" << pParm->Attribute("port") << " ";
                        std::cout << "value=" << pParm->Attribute("value") << std::endl;


                        pParm = pParm->NextSiblingElement("Input");
                    }
                }
                pParms2 = pBodys->FirstChildElement("Outputs");
                if (pParms2)
                {
                    pParm2 = pParms2->FirstChildElement("Output");
                    while (pParm2) {

                        std::cout << "port=" << pParm2->Attribute("port") << " ";
                        std::cout << "value=" << pParm2->Attribute("value") << std::endl;


                        pParm2 = pParm2->NextSiblingElement("Output");
                    }
                }



            }

            //pBodys = pRoot->NextSiblingElement("Test");
        //}
    }

    return 0;
}

DOC example.xml 
<Tests>
    <count cont="2"></count>
    <Test id="test0">
        <Inputs>
            <Input port="A" value="1" />
            <Input port="B" value="4.56" />
            <Input port="C" value="7" />        
        </Inputs>
        <Outputs>
            <Output port="D" value="10" />      
        </Outputs>
    </Test>

    <Test id="test1">
        <Inputs>
            <Input port="K" value="3" />
            <Input port="L" value="9.56" /> 
        </Inputs>
        <Outputs>
            <Output port="P" value="6" />       
        </Outputs>
    </Test>
</Tests>

The problem is that XMLElement.Attribute returns a const char* representing a C string, not an int . So this code

 (int)count->Attribute("cont");

is incorrect as you are casting a pointer to an integer (which typically results in a very large integer value, hence your apparent infinite loop).

What you need to do is convert the attribute to an int not cast it. One way to do that is to use the atoi function

int numTests = atoi(count->Attribute("cont"));
for (int i = 0; i < numTests; ++i) {

Note there is no error checking in this code (what if the attribute is missing or isn't in integral form?).

It's important to understand the difference between casting and converting one type to another. They don't always do the same thing, and just adding a cast to make a compiler error message go away is rarely the right thing to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM