【问题标题】:using TinyXML Parsing XML Elements - Infinite loop使用 TinyXML 解析 XML 元素 - 无限循环
【发布时间】:2019-06-24 20:40:22
【问题描述】:

我想读取一个 xml 文件,其中找到了几个测试,但我总是得到第一个并且它没有退出循环。 如果我一次迭代做得很好,但是当我做气泡时,我就无法进行下一个测试。

如果我使用 // pBodys = pRoot-> NextSiblingElement ("Test"); 我错过了第 2 次迭代中的错误, (https://i.gyazo.com/9a108bf422299b66abfe91127668a63c.png) 如果我不使用它,它会一直处于无限循环中

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

我无法继续前进。 对不起谷歌翻译。 问候

    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>

【问题讨论】:

  • 您应该使用 TiXmlAttribute::QueryIntValue(),如 TinyXML example中所示

标签: c++ xml parsing using tinyxml


【解决方案1】:

问题在于 XMLElement.Attribute 返回代表 C 字符串的 const char*,而不是 int。所以这段代码

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

不正确,因为您将指针转换为整数(这通常会导致非常大的整数值,因此您显然是无限循环)。

您需要做的是将属性转换int,而不是强制转换。一种方法是使用atoi 函数

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

请注意,此代码中没有错误检查(如果属性丢失或不是整数形式怎么办?)。

了解强制转换和将一种类型转换为另一种类型之间的区别很重要。它们并不总是做同样的事情,仅仅添加一个强制转换来消除编译器错误消息很少是正确的做法。

【讨论】:

  • 该裁决实际上是通过直接手动输入测试编号来解决的,因为我可以这样做。问题是当我执行 pBodys = pRoot-> NextSiblingElement ("Test");去下一个测试,但是在std :: cout Attribute ("id") i.gyazo.com/8b79d5b34e61d97d6185ccb2d38efef7.png感谢回答
猜你喜欢
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多