简体   繁体   English

C XML解析操作

[英]C XML Parsing Operations

I have a task to finish until monday but I'm stucked a point. 我有一个任务要完成,直到星期一,但我坚持了一点。 Let me explain 让我解释

  static void print_element_names(xmlNode *a_node){
  xmlNode *cur_node = NULL;
  xmlNode *cur_attr = NULL;
  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      // printf("Node Type: Element, name: %s \n Its children's type is: %d \n Its children's content is: %s \n", cur_node->name, cur_node->children->type, cur_node->children->content);

      printf("Node Name : %-20s", cur_node->name);
      if(cur_node->properties != NULL){
        for (cur_attr = cur_node->properties; cur_attr; cur_attr = cur_attr->next) {
              printf("  -> with attribute : %s\n", cur_attr->name);
      }
    }
      printf("Content %s\n", cur_node->children->content);
    }
    print_element_names(cur_node->children);
  }
  }

It is my code to write XML but I couldn't write the attributes of the nodes. 这是我编写XML的代码,但是我无法编写节点的属性。

ERROR 错误

**project1.c: In function ‘print_element_names’:
project1.c:23:23: warning: assignment from incompatible pointer type [enabled by default]
         for (cur_attr = cur_node->properties; cur_attr; cur_attr = cur_attr->next) {

It is my XML 这是我的XML

<Xset>
 <xdata>January</xdata>
 <xdata>February</xdata>
 <xdata>March</xdata>
 <xdata>April</xdata>
 <xdata>May</xdata>
 <xdata>June</xdata>
</Xset>
<Yset unit="TL" name="İzmir" showvalue="yes" fillcolor="FFCCDD">
 <ydata>1200</ydata>
 <ydata>1500</ydata>
 <ydata>7500</ydata>
 <ydata>4200</ydata>
 <ydata>5600</ydata>
 <ydata>2200</ydata>
</Yset>

Thank you all, i solved it :) 谢谢大家,我解决了:)

  static void xmlWalk(xmlNode *a_node){
    xmlNode *cur_node = NULL;  //nodes
    xmlAttr *cur_attr = NULL;  //node's attributes
    xmlChar *attribute;        //attribute values
    for (cur_node = a_node; cur_node; cur_node = cur_node->next) { //look all nodes
      if (cur_node->type == XML_ELEMENT_NODE) {      
        printf("Node Name : %-20s", cur_node->name); //print node name
        if(cur_node->properties != NULL){  //if node has attribute
          for (cur_attr = cur_node->properties; cur_attr; cur_attr = cur_attr->next) {  //search all attributes
            printf("with attribute : %s", cur_attr->name);  //print attribute tag
            attribute =  xmlNodeGetContent((xmlNode*)cur_attr);  //value is a char/char array

            printf("-> with Value: %s", attribute);   //print value
          }
        }
        printf("Content %s\n", cur_node->children->content);  //node's child's content
      }
      xmlWalk(cur_node->children);  //search node's children
    }
   }

SOLVED :) 解决了 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM