如何使用libxml c库获取XML属性?

时间:2015-11-14 13:51:16

标签: c xml xml-parsing libxml2

某项任务要求我解析XML文件并检查每个节点及其属性。我花了几周时间学习XML和XML解析。我甚至采用了与C中LIBXML解析相关的早期问题的帮助,并基于这种理解我已经在下面编写了这段代码。但是这个代码是有缺陷的,因为我没有实现这个目标。

我想我已经弄乱了一些父母和兄弟姐妹的概念。 我从下面提到的XML文件中理解的是:

Profile是Root节点,Catalog是它的子节点 目录将子项作为参数和 参数将子项作为Target 并且所有目录节点都是兄弟姐妹。

Profile--> Catalog--> Parameter-->Target
       |-> Catalog--> Parameter-->Target

但是当我尝试通过将指针移动到Catalogs子节点指针而从Catalog转到参数时,我无法继续。由于我无法达到参数,因此无法进入目标。

会欣赏我的理解和代码。 P.S我的要求是用C编码,所以请不要指向我其他语言。

/***** MY XML FILE ***************************/

<?xml version="1.0" encoding="UTF-8"?>
<!-- When VIOS level changes, the value of ioslevel needs to change  manually -->
<Profile origin="get" version="3.0.0" date="2012-10-05T00:00:00Z">
 <Catalog1 id="devParam" version="3.0">
  <Parameter1 name="policy" value="single" applyType="boot" reboot="true">
   <Target1 class="device" instance="disk1"/>
  </Parameter1>
 </Catalog1>
 <Catalog2 id="devParam" version="3.0">
  <Parameter2 name="policy" value="no" applyType="boot">
   <Target2 class="device" instance="disk2"/>
  </Parameter2>
 </Catalog2>
 <Catalog3 id="devParam" version="3.0">
  <Parameter3 name="policy" value="no" applyType="nextboot" reboot="true">
   <Target3 class="device" instance="disk3"/>
  </Parameter3>
 </Catalog3>
</Profile>

/****************************************************************/
 #include <string.h>
 #include <stdio.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>

  static void print_element_names(xmlDoc *doc, xmlNode * profile_node)
  {
     xmlNode *catalog_node = NULL, *parameter_node = NULL, *target_node = NULL, *tmp_node = NULL;
     xmlChar *instance=NULL, *key=NULL;

     if (xmlStrcmp(profile_node->name, (const xmlChar *) "Profile")) {
         fprintf(stderr,"document of the wrong type, root node != story");
         xmlFreeDoc(doc);
         return;
     }

     for (catalog_node = profile_node->xmlChildrenNode; catalog_node; catalog_node =catalog_node->next)
     {
         if (catalog_node->type == XML_ELEMENT_NODE)
         {
             printf("Catalog %s \t type %d \n",catalog_node->name, catalog_node->type);

             for(parameter_node = catalog_node->xmlChildrenNode; parameter_node; parameter_node = parameter_node->next)
             {
                 if (parameter_node->type == XML_ELEMENT_NODE)
                 {
                    printf("Parameter %s \t type %d \n",parameter_node->name, parameter_node->type);

                    for( target_node=parameter_node->xmlChildrenNode->next; target_node; target_node=target_node->next)
                    {
                         printf("Target %s \t type %d \t",target_node->name, target_node->type);

                         if((target_node->type == XML_ELEMENT_NODE)&&(!strcmp(target_node->name, (const xmlChar *)"Target")))
                         {
                              instance_attr = xmlGetProp(inner_child, "instance");
                              printf("instance_attr = %s\n",instance_attr);
                         }
                    }
                 }
             }
         }
     }
  }

  int main(int argc, char **argv)
  {
   xmlDoc *doc = NULL;
   xmlNode *root_element = NULL;

   if (argc != 2)  return(1);

   /*parse the file and get the DOM */
   if ((doc = xmlReadFile(argv[1], NULL, 0)) == NULL){
        printf("error: could not parse file %s\n", argv[1]);
        exit(-1);
   }

   /*Get the root element node */
   root_element = xmlDocGetRootElement(doc);
   print_element_names(doc, root_element);


   xmlFreeDoc(doc);       /* free document*/
   xmlCleanupParser();    /*/ Free globals*/
   return 0;
  }

1 个答案:

答案 0 :(得分:3)

我最近对XML DOM的理解工作得很好,它说元素的所有属性都在DOM中表示为该元素的子节点。因此,如果制作上述XML的DOM,我们将看到:

                            Profile
   ___________________________|____________________
  |        |         |        |          |         |
 date    origin   version   catalog1  catalog2   catalog3
    __________________________|
   |              |           |
parameter    version         id
   |_________________________________________
   |         |        |           |          |
  name      value   applytype    reboot     target
                                    __________|
                                   |          |
                              instance      class

目录2和目录3也将有他们的孩子。 基于这个DOM,如果我已经写了get_next_node和get_children_node函数,它对我来说很好。

/**
 * get the children node and skip any non xml element node
 *
 * @param  xml node
 * @param  xml children node
 *
 * @return xmlNodePtr children of xml node
 */

 static void xmlGetNodeChildren(xmlNodePtr xmlNode, xmlNodePtr *childrenNode)
 {
      xmlNodePtr node = NULL;

      node = xmlNode->children;
      while (node->type != XML_ELEMENT_NODE)
      {
         node = node->next;
         if (node == NULL)
         {
            break;
         }
      }

      *childrenNode = node;
 }


/**
 * get the next node and skip any non xml element node
 * such as text and comment node
 *
 * @param  xml node
 * @param  xml next node
 */
 static void xmlGetNodeNext(xmlNodePtr *xmlNode)
 {
   xmlNodePtr node = NULL;

   node = (*xmlNode)->next;
   while (node->type != XML_ELEMENT_NODE)
   {
       node=node->next;
       if (node == NULL)
       {
          break;
       }
   }
   *xmlNode = node;
}
相关问题