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 に答える