Reputation: 907
I have an xml file which I have to read and parse to find out one value.
I have this value mentioned within tags at two places in the file as follows:
<length>xx</length>
<length type = "float">yy</length>
I need to extract xx and yy. I am using simple string functions(owing to size restrictions I cant use xml Parsers).
What string functions would help me extract xx and yy?
I tried strtok() on these lines but without success..:
fp = fopen( "trial.xml", "r" );
if(fp == NULL){
perror("file missing");
}
while (fgets (buffer, sizeof (buffer), fp) != NULL) {
char *p;
p = strstr(buffer, "<length");
if(p != NULL){
printf("p = %s\n", p);
p = strtok (p, "<>");
printf("strtok 1, p = %s\n", p);
p = strtok (NULL, "<>");
printf("p = %s\n", p);
}
Upvotes: 0
Views: 2607
Reputation: 1008
I would suggest using TinyXML to do your parsing.
If you want to use brute force...
fp = fopen( "trial.xml", "r" );
if(!fp) {
perror("file missing");
}
while(fgets (buffer, sizeof (buffer), fp)) {
if(strstr(buffer, "<length>")) {
char* start = strchr(buffer, '>');
start++;
char* end = strchr(buffer, '<');
end = '\0';
printf("%s\n", start); // prints xx
}
if(strstr(buffer, "<length type = \"float\">")) {
char* start = strchr(buffer, '>');
start++;
char* end = strchr(buffer, '<');
end = '\0';
printf("%s\n", start); // prints yy
}
}
Upvotes: 2