2016-10-19 31 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的,我们可以把它保存在一个数组。