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

查看:23
本文介绍了解析两个 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] = '';
        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天全站免登陆