我有一个简单的xml文件,存储在char []。
中<?xml-stylesheet type='text/xsl' href='http://prova'?>
<ns2:operation-result xmlns:ns1="http://www.w3.org/1999/xlink" xmlns:ns2="http://www.prova.it/pr/a" operation-start="2015-01-12T15:22:46.890+01:00" operation-end="2015-01-12T15:22:46.891+01:00"><ns2:error code="ROSS-A001"><ns2:msg>Error</ns2:msg></ns2:error></ns2:operation-result>
我需要一个简单的C例程来仅提取错误代码(在本例中为ROSS-A001)和错误信息之间并将其放在两个char []中。
我该怎么做?
非常感谢
答案 0 :(得分:1)
怎么样?
char *extractErrorCode(const char *xml)
{
char *pointer;
char *result;
char *tail;
size_t length;
/* advance the pointer to the = character, and skip the " -> +1 */
pointer = strstr(xml, "error code=") + strlen("error code=") + 1;
result = NULL;
if (pointer == NULL)
return NULL;
length = 0;
tail = strchr(pointer, '>');
if (tail == NULL)
return NULL;
/* -1 skip the trailing " */
length = tail - pointer - 1;
if (length > 0)
{
result = malloc(1 + length);
if (result == NULL)
return NULL;
result[length] = '\0';
memcpy(result, pointer, length);
}
return result;
}
如果不是NULL