2011-05-01 70 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