天天看点

列出一个HTML文件的所有标签

libxml 列出一个HTML文件的所有标签。

#include <cstdio>

#include <libxml/HTMLparser.h>

#include <libxml/tree.h>

#include <iostream>

#include <cstring>

using namespace std;

static void print_element_names(htmlNodePtr a_node)

{

    htmlNodePtr cur_node = NULL;

    for (cur_node = a_node; cur_node!=NULL; cur_node = cur_node->next) {

            printf("node type: Element, name: %s\n", cur_node->name);

        print_element_names(cur_node->children);

    }

}

int main(int argc, char **argv) {

  htmlDocPtr doc;

  htmlNodePtr root_node;

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

  root_node = xmlDocGetRootElement(doc);

  print_element_names(root_node);

    xmlFreeDoc(doc);

    xmlCleanupParser();

    return 0;

}

编译 g++ `xml2-config --cflags --libs` dom_html_libxml_test.cpp

文出  http://zh-cn.w3support.net/index.php?db=so&id=807171

libxml 其他介绍

http://www.blogjava.net/wxb_nudt/archive/2008/01/29/161340.html