如何获取标记块 libxml2 中的文本?

How do I get the text at a tag block libxml2?

我有htmlDocPtr htmlfile = htmlParseFile(localfileurl, NULL).

Local Html file

 <!DOCTYPE html>
<html>
<head>
<meta></meta>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html> 

我想最终将 页面标题 存储在 char variable

我试过了

htmlNodePtr node = xmlDocGetRootElement(htmlfile);
// title is on the following
node = node->children->next->children->next->next->next;

我现在如何获取标题的值

遍历文档,查找名为“title”的元素,并获取其内容:

static void printTitle(xmlDoc *doc, xmlNode * a_node)
{
    xmlNode *cur_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE && !xmlStrcmp(cur_node->name, (const xmlChar *)"title")) {
            xmlChar* content;
            content = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
            printf("node type: Element, name: %s, content: %s\n", cur_node->name, content);
            xmlFree(content);
        }

        printTitle(doc, cur_node->children);
    }
}


int main(int argc, char **argv)
{
    xmlDoc *doc = NULL;
    xmlNode *root_element = NULL;

    if (argc != 2)
        return(1);

    LIBXML_TEST_VERSION

    doc = xmlReadFile(argv[1], NULL, 0);

    if (doc == NULL) {
        printf("error: could not parse file %s\n", argv[1]);
    }

    root_element = xmlDocGetRootElement(doc);

    printTitle(doc, root_element);

    xmlFreeDoc(doc);

    xmlCleanupParser();

    return 0;
}

(见Retrieving Element Content