我有去大致是这样的XML文件:空元素造成的libxml段错误
<root>
<children>
<child />
</children>
<otherchildren>
</otherchildren>
<morechildren>
<child />
</morechildren>
</root>
及这里的分析它的C:
doc = xmlDocPtr;
doc = xmlParseFile(file);
cur = xmlNodePtr;
cur = xmlDocGetRootElement(doc);
cur = cur->xmlChildrenNode;
while (cur != NULL){ // Loop through children of root
// Do my thing
cur = cur->next;
}
当的xmlNodePtr来otherchildren,因为有没有内容,下一个节点(文本节点)是NULL
这给了我一个问题,与我的循环混乱,并以某种方式导致段错误。
我该如何解决这个问题,而不是明显的if语句?在otherchildren
下面有更多的xml,如果循环退出,我不能得到它。
如果您使用 ,该怎么办?不知道这是否在你的情况下是可能的。 –
Gerben
2011-05-01 11:33:40
不,总的来说,其他孩子应该拥有元素,但有时候它不会,我的程序需要处理这个问题。 – 2011-05-01 12:46:12
我不明白你在做什么,不认为这是有道理的。 XML不是一个列表,而是一棵树。你不能通过在每一步跟随一个“下一个”指针来走它。您需要能够备份树,可以使用递归或追踪您的位置来备份。 – 2011-05-01 14:06:38