views:

467

answers:

1

I'm using this program to display a list of all html tags in a given file:

#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;

}

How do I get it to display the attributes as well (eg. href="something" for )?

A: 

It seems there is no such field:

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’

=== EDIT ===

If I do something like this:

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

I get the name of the attribute - "href"

If I go one step further:

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

I get "text", but not the actual link.