C XML 解析操作

标签 c xml

我有一项任务要在周一之前完成,但我遇到了一个问题。让我解释一下

  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);
  }
  }

这是我编写XML的代码,但我无法编写节点的属性。

错误

**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) {

这是我的 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>

最佳答案

谢谢大家,我已经解决了:)

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

已解决:)

关于C XML 解析操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677523/

相关文章:

计算哈希表中字符串的个数

c - 使用指针获取结构中动态int数组的值

c - 使用 %C、%S、%ls、%lc 时 printf 返回 -1 的示例

java - 安卓 : Parse XML with Sax failed (goes to exception)

xml - 根据同胞的值选择 XML 元素

Java - 具有可选字段的 JAXB XML 解码

android - 如何从服务器获取数据并用它更新数据库?

fnctl(F_SETOWN, <pid>) 能否将信号定向到 pthread ID 而不是进程 ID?

xml - 多个xsl选择条件

c - 当需要在其他语言(如 C++)中抛出异常时,在 C 中返回什么?