【问题标题】:How I can compare node name libxml2?如何比较节点名称 libxml2?
【发布时间】:2012-12-25 17:37:17
【问题描述】:

我使用 g++ 和 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;
}

结果是

地址簿

名字

地址

街道

城市

状态

压缩

电话


它不等于“城市”可能是因为 xmlChar 和 char。 如何比较 nodename 和 char ?

【问题讨论】:

    标签: xml libxml2


    【解决方案1】:

    您的convert(cur_node)==c 是比较指针而不是比较字符串内容,这不是在 C++ 中比较字符串的方法。

    尝试使用 strncmp 比较两者。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多