2011-05-01 69 views
0

我有去大致是这样的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,如果循环退出,我不能得到它。

+0

如果您使用,该怎么办?不知道这是否在你的情况下是可能的。 – Gerben 2011-05-01 11:33:40

+0

不,总的来说,其他孩子应该拥有元素,但有时候它不会,我的程序需要处理这个问题。 – 2011-05-01 12:46:12

+0

我不明白你在做什么,不认为这是有道理的。 XML不是一个列表,而是一棵树。你不能通过在每一步跟随一个“下一个”指针来走它。您需要能够备份树,可以使用递归或追踪您的位置来备份。 – 2011-05-01 14:06:38

回答

0

使用您的.xml,我得到:

./SOTrials ../Untitled1.xml 
../Untitled1.xml:10: parser error : expected '>' 
</root> 
^ 
error: could not parse file ../Untitled1.xml 
Speicherzugriffsfehler 

(Speicherzugriffsfehler =内存故障)

改变.XML到

... 
</morechildren> 
... 

给出:

./SOTrials ../Untitled1.xml 
node type: Element, name: text 
node type: Element, name: children 
node type: Element, name: text 
node type: Element, name: otherchildren 
node type: Element, name: text 
node type: Element, name: morechildren 
node type: Element, name: text 

看起来你的代码是b etter比你的xml。

+0

哈,很好,但这不是问题。 – 2011-05-01 16:01:46

+0

@J V:那问题是什么? (毕竟其他孩子被捡到后会有更多的孩子) – 2011-05-01 16:15:51

+0

我不知道,我认为这是问题,因为在段错误之前我没有看到文本元素'printf()'d。我遇到了另一个问题,使我更加具体地停止了测试,一旦清理完成,我会在几分钟内回来。 – 2011-05-01 16:18:34