2016-10-19 18 views
2

我需要從節點中的文本中獲取一個字符串數組,該節點本身由xml文件中的其他元素剪切。我使用libxml2庫在C中工作。XML:從文本中獲取字符串數組

例:
<option>some text <middletag />other text</option>

我試着用xmlNodeGetContent(xmlnode);但我只得到一個字符串像"some text other text"

問題是:是否有可能得到一個字符串數組,這個例子將是{"some text ", "other text"}

回答

4

我找到了解決方案,我不得不說我感到羞愧,因爲我花了太多時間才找到它。

這很簡單,我再次藉此爲例:

<option>some text <middletag />other text</option> 

有了這個,你可以有<option>節點上的xmlnode *。我們可以在列表xmlnode->children上找到帶有循環的部分some text <middletag />other text。我們只需要尋找類型爲XML_TEXT_NODE的節點並獲取內容。

代碼:

xmlNode *node = option_node->children; 
for (; node; node = node->next){ 
    if (node->type == XML_TEXT_NODE) { 
     printf("%s\n", node->content); 
    } 
} 

結果:

some text 
other text 

現在,使用malloc/realloc的,我們可以把它保存在一個數組。