解析XML文件的一部分

时间:2013-05-21 13:36:38

标签: c xml libxml2

所以我现在坚持了很长一段时间。我使用了libxml2,它对代码中的另一部分非常有用,但是我似乎无法想象这一部分而且它会像疯了一样烦扰我。

我有这个xml代码:

<MetaCommandSet>
  <MetaCommand name="ChangeSystemInterval">
    <ArgumentList>
      <Argument name="Interval" argumentTypeRef="UnsignedByteType"></Argument>
    </ArgumentList>
  </MetaCommand>

现在我想要的是命令的名称及其参数。这意味着它必须保持在相同的元命令中,以便我可以收集参数,而不是将其保存到例如结构。

代码段:

   for (cur_node = a_node; cur_node; cur_node = cur_node->next)
   {
    DATA TM_tmp;
    COMMAND TC_tmp;

    if(!xmlStrcmp(cur_node->name, "MetaCommand"))
    {
        TC_tmp.functionName = malloc(strlen((xmlGetProp(cur_node, "name") + 1)));
        TC_tmp.functionName = xmlGetProp(cur_node, "name");
        printf("Name: %s\n",TC_tmp.functionName);
        /*
          It now needs to keep looping this so i get every argument but it cant find any childs after argumentList

        */
    }

    createArray(cur_node->children);

我比获取MetaCommand名称更进一步。我已经循环抛出所有元素,如果它是一个MetaCommand元素,我想继续我上面所说的。

请给我一些想法

1 个答案:

答案 0 :(得分:0)

取自我的一个源代码。你必须潜入比我更深的一层,我有&lt;定价和GT;&LT;价格age_group =“”&gt;&lt; EUR&GT; ...&LT; / EUR&GT;&LT; /定价&GT;&LT; /价格&gt;

{
while (cur != NULL) {
  if (xmlStrcmp(cur->name, (const xmlChar *) "pricing") == 0) {
    cur = cur->xmlChildrenNode;

    while (cur != NULL) {
      if (xmlStrcmp(cur->name, (const xmlChar *) "price") == 0) {
        xmlNodePtr node = cur->xmlChildrenNode;

        if (*kopf == NULL) {
          *kopf = (Pricing *)HeapMalloc(sizeof(Pricing), "Pricing", 0);
          ret = *kopf;
        } else {
          ret->next = (Pricing *)HeapMalloc(sizeof(Pricing), "Pricing", 0);
          ret = ret->next;
        }

        parseAttribut(cur,      "age_group",        &(ret->age_group),   fehler);
        parseElement(doc, node, "EUR",              &(ret->price),       fehler);
      }
      cur = cur->next;
    }
    break;
  }
  cur = cur->next;
}
}


void parseAttribut(xmlNodePtr node, const xmlChar *str, xmlChar **dest, short muss, short *fehler)
{
  *dest = xmlGetProp(node, str);
  return;
}

void parseElement(xmlDocPtr doc, xmlNodePtr node, const xmlChar *str, xmlChar **dest,  short muss, short *fehler)
{
  short found = FALSE;

  while (node) {
    if (xmlStrcmp(node->name, str) == 0) {
      *dest = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      found = TRUE;
      break;
    }
    node = node->next;
  }
  return;
}