解析两个XML标记之间的值 [英] parsing the value in between two XML tags

查看:300
本文介绍了解析两个XML标记之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经有人问过这个问题,但是我似乎找不到合适的解决方案,所以我要说明问题:

I know this one has been asked before, however I can't seem to find a suitable solution, so I 'll state the problem:

我有一个类似于XML文件的字符串.它不是XML字符串,但具有开始和结束标签.所有信息都驻留在一行中,例如:

I have a string of characters that is similar to an XML file. It's not an XML string, but it has opening and closing tags. All the information resides in one single line, for example:

<user>username</username>random data;some more random data<another tag>data</anothertag>randomdata;<mydata>myinfo</mydata>some more random data....

等...

我仅尝试阅读<mydata></mydata>之间的内容.有什么办法可以解析吗?

I am trying to read ONLY what's in between <mydata></mydata>. Any way to just parse this?

感谢,感谢代码.

推荐答案

我只会使用strstr():

I would just use strstr():

char * get_value(const char *input)
{
  const char *start, *end;

  if((start = strstr(input, "<mydata>")) != NULL)
  {
    start += strlen("<mydata>");
    if((end = strstr(start, "</mydata>")) != NULL)
    {
      char *out = malloc(end - start + 1);
      if(out != NULL)
      {
        memcpy(out, start, (end - start));
        out[end - start] = '\0';
        return out;
      }
    }
  }
  return NULL;
}

请注意,以上内容未经测试,直接写在SO编辑框中.因此,几乎可以保证包含至少一个一对一的错误.

Note that the above is untested, written directly into the SO edit box. So, it's almost guaranteed to contain at least one off-by-one error.

这篇关于解析两个XML标记之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆