简体   繁体   English

获取libxml2的内容

[英]Get content with libxml2

I'm trying to use libxml2's function xmlNodeGetContent to acess content from an xml file, but for some reason content within self-closing tabs can't be read. 我正在尝试使用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;
}

I'm using this sample xml file with both types of tabs and the first row tab can be read while the second one returns (null); 我正在使用这个示例xml文件和两种类型的选项卡,第一行选项卡可以读取,而第二行选项卡返回(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>

The second row element is empty, it has no content, only attributes. 第二行元素为空,它没有内容,只有属性。

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

Without any schema, it's impossible for the framework to know what type the second tag element is and it can't have a default value defined for it, and it can't derive a type, therefore it has no content. 如果没有任何模式,框架就不可能知道第二个标记元素是什么类型,并且它不能为它定义默认值,并且它不能派生类型,因此它没有内容。 It does have a content attribute however, but you access attributes using different methods in most of the frameworks I am aware of. 它确实有一个内容属性,但你在我所知道的大多数框架中使用不同的方法访问属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM