使用 libxml2 获取内容

Get content with libxml2

我正在尝试使用 libxml2 的函数 xmlNodeGetContent 来访问 xml 文件中的内容,但由于某些原因,自动关闭选项卡中的内容无法访问被阅读。

void parseRow (xmlDocPtr doc, xmlNodePtr cur) {

    xmlChar *key;
    cur = cur-> xmlChildrenNode;
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"row"))) {
            key = xmlNodeGetContent( cur->children );
            printf("keyword: %s\n", (char*)key);
            xmlFree(key);
        }
        cur = cur -> next;
    }
    return;
}

我正在使用此示例 xml 文件,其中包含两种类型的选项卡,第一行选项卡可以读取,而第二行选项卡 returns (null);

<?xml version="1.0"?>
<story>
  <storyinfo>
    <author>John Fleck</author>
    <datewritten>June 2, 2002</datewritten>
    <row> Id="1" UserId="1" </row>
    <row Id="1" UserId="1" />
    <keyword>Id="hello"</keyword>
  </storyinfo>
</story>

第二行元素为空,没有内容,只有属性。

<tag>non-empty content</tag>
<tag content="attribute value"/>

没有任何模式,框架不可能知道第二个标签元素是什么类型,也不能为它定义默认值,也不能派生类型,因此它没有内容.但是它确实有一个内容属性,但是在我所知道的大多数框架中,您可以使用不同的方法访问属性。

如果要获取属性值,使用xmlGetProp。