Search code examples
xmllibxml2

How I can compare node name libxml2?


I use g++ and libxml2

static void print_element_names(xmlNode * a_node,xmlDoc * doc) {               
  xmlNode *cur_node = NULL;
  const char *c= "city";
  xmlChar *name;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      std::cout<<convert(cur_node)<<std::endl;
    }

    if (convert(cur_node)==c){
      //work but not equal with city
      std::cout<<"Found node"<<std::endl;
    }
    print_element_names(cur_node->children,doc);
  }
}

char * convert(xmlNode * a_node) {
  char* a = (char *)a_node->name;
  return a;
}

result is

address_book

person

name

address

street

city

state

zip

phone


It not equal with "city" maybe becuase xmlChar and char. How can I compare nodename with char ?


Solution

  • Your convert(cur_node)==c is comparing pointers instead of comparing string contents, which isn't the way to compare strings in C++.

    Try using strncmp to compare the two.

    http://www.cplusplus.com/reference/cstring/strncmp/