开发者

Using expat to parse xml

I need to get attributes name and values of an xml file with many elements.

what is the best way to capture the attribute values in a class?

I have to following code for the startelement handler:

start(void *data, const char *el, const char **attr)
{
  int i;

  // Skip the ParameterList element
  if(strcmp(el, "ParameterList") == 0)
  {
      Depth++;
      return;
  }
  //for (i = 0; i < Depth; i++)
    //printf("  ");

  //printf("%s", el);
  DEMData demData;
  for (i = 0; attr[i]; i += 2)
  {
      if(strcmp(attr[i],"BitFldPos") == 0)
      {
      demData.SetBitFldPos(*attr[i + 1] - '0');
      }
      else if(strcmp(attr[i],"BytFldPos") == 0)
      {
        char* pEnd;
        int tmp = strtol(attr[i + 1],&pEnd,10);
        demData.SetBytFldPos(t开发者_开发知识库mp);
      }
      else if(strcmp(attr[i],"ByteOrder") == 0)
      {
        demData.SetByteOrder(attr[i + 1]);
      }
      else if(strcmp(attr[i],"DesS") == 0)
      {
      demData.SetDesS(attr[i + 1]);
      }
      else if(strcmp(attr[i],"EngUnit") == 0)
      {
        demData.SetEngUnit(attr[i + 1]);
      }
      else if(strcmp(attr[i],"OTag") == 0)
      {
        demData.SetOTag(attr[i + 1]);
      }
      else if(strcmp(attr[i],"ValTyp") == 0)
      {
        demData.SetValType(attr[i + 1]);
      }
      else if(strcmp(attr[i],"idx") == 0)
      {
        char* pEnd;
        int tmp = strtol(attr[i + 1],&pEnd,10);
        demData.SetIndex(tmp);
      }
      //printf(" %s='%s'", attr[i], attr[i + 1]);
  }


  // Insert the data in the vector.
  dems.push_back(demData);

  Depth++;
}


I would recommend an STL vector of STL pairs of strings. first is the attribute name, second is the value.

std::vector<std::pair<std::string,std::string> >

Some would suggest an std::map.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜