c - XML : Get an array of string from text cut by element

标签 c xml libxml2

我需要从节点中的文本获取一个字符串数组,该节点本身被 xml 文件中的其他元素剪切。我使用 C 语言使用 libxml2 库。

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

我尝试过 xmlNodeGetContent(xmlnode);但我只得到像 "some text other text" 这样的字符串.

问题是:是否有可能获得一个字符串数组,在这个例子中,它是 {"some text ", "other text"}

最佳答案

我已经找到了解决方案,但我不得不说我感到羞愧,因为我花了太多时间才找到它。

很简单,我再举这个例子:

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

有了这个你就可以拥有xmlnode *关于<option>节点。我们可以找到some text <middletag />other text部分列表中有一个循环 xmlnode->children 。我们只需寻找类型为 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,我们可以将其保存在数组中。

关于c - XML : Get an array of string from text cut by element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40134764/

相关文章:

xml - 如何使用 XSLT 删除这个棘手的 XML 重复节点?

C libxml2 xmlTextWriter 标签随机嵌套

c++ - 在 C++ 中从 libxml++ 中的字符串加载 XML

c - 不同类型的链表!

const 导致 "formal parameter different from declaration"警告

java - Android:将 XML android:background 转换为 Java 程序代码

iphone - libXML 轻松的 HTML 解析

python - Python 的 C 扩展不起作用

C API返回指定时区的时间字符串?

java - 要在 Standalone.xml 文件中进行哪些更改,以排除特定目录在 JBoss Server[Wildfly 9xx] 中自动部署