0

所以我已经坚持了很长一段时间了。我使用 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 元素,我想继续我上面所说的。

请给我一些想法

4

1 回答 1

0

taken from one of my source coeds. You have to dive one level deeper than me, i have < pricing>< price age_group="">< EUR>...< /EUR>< /pricing>< /price>

{
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;
}
于 2013-05-21T15:00:20.050 回答