我想知道如何循环我做的解析器。我有几个文本文件,我不知道该怎么做。这是代码。循环解析器
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int parse(char **argv)
{
/* code that converts a text file to a string called file_contents */
char *target = NULL;
char *target2 = NULL;
char *start, *end;
const char *tag1 = "<item>";
const char *tag2 = "</item>";
if(start = strstr(file_contents, tag1))
{
start += strlen(tag1);
if(end = strstr(start, tag2))
{
target = (char *)malloc(end-start+1);
memcpy(target, start, end-start);
target[end - start] = '\0';
}
const char *tag3 = "<title>";
const char *tag4 = "</title>";
if(start = strstr(target, tag3))
{
start += strlen(tag3);
if(end = strstr(start, tag4))
{
target2 = (char *)malloc(end-start+1);
memcpy(target2, start, end-start);
target2[end-start] = '\0';
printf("%s\n", target2);
}
}
/* same code for other tags */
}
}
free(target);
return 2;
}
这是一个文本的示例。
<item>
<title>blah blah</title>
<otherTags>blah blah</otherTags>
</item> <item>
<title>blah blah</title>
<otherTags>blah blah</otherTags>
</item> <item>
<title>blah blah</title>
<otherTags>blah blah</otherTags>
</item>
我的代码只解析第一项。我是一个新手,所以引导我。谢谢。
那么,一个,你的标题说“循环”。这几乎涉及所有情况......一个*循环*。尝试一个? – WhozCraig
我不知道该把它放在哪里:(还有条件 – estudyante
我希望为了你自己的缘故,你正在把解析器当做学习练习,因为像你这样的标记文件的解析器已经存在。只不过是简化的XML,这意味着几乎任何XML解析器都应该能够处理它 –