Berkin
Berkin

Reputation: 1664

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.

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

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

Upvotes: 0

Views: 186

Answers (1)

Berkin
Berkin

Reputation: 1664

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 :)

Upvotes: 1

Related Questions