使用C在LibXml2中奇怪的树遍历行为

时间:2015-11-18 14:31:13

标签: c xml tree libxml2

我有一个XML文件,其中的设备结构如下:

<Device>
   <ID></ID>
   <VAL1></VAL1>
   <VAL2></VAL2>
   <VAL3></VAL3>
   <VAL4></VAL4>
   <VAL5></VAL5>
   <VAL6></VAL6>
</Device>

我的函数用于查找第一个<Device>元素,并循环遍历设备的子元素,并确保不存在任何意外标记。

  

注意:此函数使用XML_PARSE_NOBLANKS打开XML文件   选项和curNode只有在它是XML_ELEMENT_NODE

时才会被检查
// If current node is opening Device tag for a device
if (xmlStrcmp(curNode->name, (const xmlChar*)"Device") == 0)
{
   // Loop through child nodes and assure that they match
   // a valid tag and have no other children tag elements
   childNode = curNode->children;

   while (childNode != NULL)
   {
      if (childNode->type == XML_ELEMENT_NODE)
      {
         for (nIdx = 0; nIdx < 7; nIdx++)
         {
            if (xmlStrcmp(childNode->name, 
                         (const xmlChar*)acXmlChildTags[nIdx]) == 0)
            {
               bChildFound = TRUE;
               break;
            }
         }
         if (bChildFound != TRUE)
         {
            nReturnValue = FAILURE;
            break;
         }
      }
      childNode = xmlNextElementSibling(childNode);
   }
}

acXmlChildTagschar*的数组,等于标记字符串(“ID”,“VAL1”等)

除了这里的整体逻辑中的任何问题,因为函数的某些部分被排除在外,我注意到的是我的函数正确到达“设备”内部的“VAL6”元素,但是当我转到下一个元素它跳回“VAL3”,然后继续回到“VAL6”,然后最终到达NULL的下一个元素,以打破循环。

有人可以向我解释为什么要去“Device”的最后一个子元素的下一个元素兄弟将我推回到第3个子元素?

我为这个令人费解的问题提前道歉。

0 个答案:

没有答案