MiniXML:在C中解析xml

问题描述:

我使用Minixml解析C中的xml文件。我想从这个小的例子中得到值“check-time”,“check-key”等。文件:MiniXML:在C中解析xml

<?xml version="1.0"?> 
<!--Test--> 
<myfile> 
    <command type="start"> 
     <instance>check-time</instance> 
     <instance>check-key</instance> 
     <instance>check-position</instance> 
     <action type="press button"> 
     <blue>1</blue> 
    </action> 
    </command> 

</myfile> 

这是我的代码。我不知道如何通过标签提取数据。请帮忙。为什么subnode-> next没有显示下一个实例值?

fp = fopen("trial.xml", "r"); 
     if(fp == NULL){ 
      perror("file missing"); 
     } 

     mxml_node_t *tree, *Asset_elem; 
     tree = mxmlLoadFile(NULL, fp,MXML_TEXT_CALLBACK); 

     fclose(fp); 

     Asset_elem = mxmlWalkNext(tree, tree, MXML_DESCEND_FIRST); 

     if(tree != NULL){ 
     mxml_node_t *node, *subnode, *subsubnode; 

      for (node = mxmlFindElement(Asset_elem, tree, 
             "command", 
             "type", "start", 
             MXML_DESCEND); 
       node != NULL; 
       node = mxmlFindElement(node, Asset_elem, 
         "command", 
         "type", "start", 
             MXML_DESCEND)) 
     { 

     printf("Inside for loop\n"); 
      printf("node name= %s\n", node->value.element.name); 

      if(node){ 
       subnode = mxmlFindElement(node, tree, "instance", NULL, NULL, MXML_DESCEND); 
       if(subnode != NULL){ 
        printf("subnode name= %s\n", subnode->value.element.name); 
        subsubnode = subnode->child; 
        printf("subsubnode name= %s\n", subsubnode->value.text.string); 
        subsubnode = subnode->next; 
        printf("subsubnode name= %s\n", subsubnode->value.text.string); 

       } 
      } 
     } 
+1

很高兴看到您的代码段的实际输出。 – Clemens

现在它已经不使用子节点,你可以尝试使用node->child->value.xxx。祝你好运。