0

我正在使用这个程序来显示给定文件中所有 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;

}

我如何让它也显示属性(例如href="something"for <a>)?

4

1 回答 1

0

似乎没有这样的领域:

zajec@linux-lbnn:~/Prog_zesp> g++ `xml2-config --cflags --libs` -o tester tester.cpp
tester.cpp: In function ‘void print_element_names(xmlNode*)’:
tester.cpp:17: error: ‘struct _xmlNode’ has no member named ‘attributes’

=== 编辑 ===

如果我做这样的事情:

if (strcmp((char *)cur_node->name, "a")==0) {
        cout << cur_node->properties->name << endl;

我得到属性的名称 - “href”

如果我更进一步:

if (strcmp((char *)cur_node->name, "a")==0) {
            cout << cur_node->properties->children->name << endl;

我得到“文本”,但不是实际链接。

于 2009-04-30T14:39:34.390 回答