Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 19:12

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: parsing XML file in C
PostPosted: 19 Oct 2008 01:52 

Joined: 01 Sep 2008 07:40
Posts: 31
i'm looking for a fairly straight forward way of parsing a basic XML file into an array, ive been trying to use strtok() but it is furiously frustrating. all i need to do is parse the dumb <values> and </values>

any help is much appreciated


Top
 Profile  
 
 
 Post subject: Re: parsing XML file in C
PostPosted: 19 Oct 2008 12:07 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
text files are a pain in C.
If the XML you want to parse is simple (like in the example you have posted) you can use a combination of pointers and strstr although probably this solution is not much different than strtok (most depends by your input file if <values> are one for each line or mixed and so on).
The alternative is using libxml bug I have never tried it.


Top
 Profile  
 
 Post subject: Re: parsing XML file in C
PostPosted: 21 Oct 2008 01:34 

Joined: 24 Feb 2008 08:31
Posts: 10
Probably the most basic (but complete) library for parsing XML would be Expat: http://expat.sourceforge.net/

I've never used it personally, but it should work for what you're trying to do (and it's fairly lightweight too).


Top
 Profile  
 
 Post subject: Re: parsing XML file in C
PostPosted: 21 Oct 2008 04:27 

Joined: 01 Sep 2008 07:40
Posts: 31
ty AC, I found a simple solution online that i was able to modify for my needs, here is the original function (credit: happycoding.com author?)

Code:
char **split(char *string, char *delim) {
   char **tokens = NULL;
   char *working = NULL;
   char *token = NULL;
   int idx = 0;

   tokens  = malloc(sizeof(char *) * MAXTOKENS);
   if(tokens == NULL)
      return NULL;

   working = malloc(sizeof(char) * strlen(string) + 1);
   if(working == NULL)
      return NULL;

   strcpy(working, string);

   for(idx = 0; idx < MAXTOKENS; idx++)
      tokens[idx] = NULL;

   token = strtok(working, delim);
   idx = 0;

   while((idx < (MAXTOKENS - 1)) && (token != NULL)) {
      tokens[idx] = malloc(sizeof(char) * strlen(token) + 1);

      if(tokens[idx] != NULL) {
         strcpy(tokens[idx], token);
         idx++;
         token = strtok(NULL, delim);
      }
   }

   free(working);
   return tokens;
}


Top
 Profile  
 
 Post subject: Re: parsing XML file in C
PostPosted: 21 Oct 2008 12:45 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
some small tips which make the code more readable and shorter:
- you can avoid to use malloc + strcpy simply using strdup, example:
working = strdup(string);
tokens[idx] = strdup(token);
- you can allocating and zeroing tokens using: tokens = calloc(sizeof(char *), MAXTOKENS);
- if "tokens[idx] != NULL" do that stuff otherwise break or you could have an endless loop


Top
 Profile  
 
 Post subject: Re: parsing XML file in C
PostPosted: 22 Oct 2008 04:20 

Joined: 01 Sep 2008 07:40
Posts: 31
aluigi wrote:
some small tips which make the code more readable and shorter:
- you can avoid to use malloc + strcpy simply using strdup, example:
working = strdup(string);
tokens[idx] = strdup(token);
- you can allocating and zeroing tokens using: tokens = calloc(sizeof(char *), MAXTOKENS);
- if "tokens[idx] != NULL" do that stuff otherwise break or you could have an endless loop

ty for the advice, will do ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: