Björn Ternes
Björn Ternes

Reputation: 1127

libxml how to get child atribute

How to get the value of a child atribute? For example the xml:

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

How i get Lala? I try this (but it doesnt work :())

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: %s\n", 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 document\n");
        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;
}

When i start this Code i get not LaLa, i get null.

Upvotes: 0

Views: 994

Answers (2)

Remi Gacogne
Remi Gacogne

Reputation: 4853

You are using xmlGetProp(), which retrieves the value of an attribute of the current node, but "info" is a child, not an attribute, of the "child" node. What you should be using is xmlNodeGetContent() on the "info" node.

Upvotes: 1

fsquirrel
fsquirrel

Reputation: 832

it looks like misprint. you should use (const xmlChar *)"child") instead of (const xmlChar *)"cild")

Upvotes: 0

Related Questions