libxml如何获取子atribute

libxml how to get child atribute

本文关键字:atribute 获取 何获取 libxml      更新时间:2023-10-16

如何获取child atributevalue?例如xml:

<root>
   <child>
      <info>Lala</info>
   </child>
</root>

如何获得Lala?我试过这个(但不起作用:())

void parsePackage (xmlDocPtr doc, xmlNodePtr cur) {
        xmlChar *key;
        xmlChar *uri;
        cur = cur->xmlChildrenNode;
        while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"child"))) {
            key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
            uri = xmlGetProp(cur, (const xmlChar *)"info");
            printf("Info: %sn", uri);
            xmlFree(key);
        }
            cur = cur->next;
        }
    return;
}
void parseDoc() {
    xmlDocPtr doc;
    xmlNodePtr cur;
    doc = xmlParseFile("test.xml");
    if (doc == NULL ) {
        fprintf(stderr,"Document not parsed successfully. n");
        return;
    }
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"empty documentn");
        xmlFreeDoc(doc);
        return;
    }
    if (xmlStrcmp(cur->name, (const xmlChar *) "root")) {
        fprintf(stderr,"document of the wrong type, root node != package");
        xmlFreeDoc(doc);
        return;
    }
    parsePackage(doc, cur);
    xmlFreeDoc(doc);
    return;
}

当我开始这个代码时,我得到的不是LaLa,而是null

您使用的是xmlGetProp(),它检索当前节点的属性值,但"info"是"child"节点的子节点,而不是属性。您应该在"info"节点上使用xmlNodeGetContent()。

它看起来像印刷错误。你应该使用(const xmlChar *)"child")而不是(const xmlChar *)"cild")