libxml C fun

Billy O'Connor billyoc at linuxfromscratch.org
Mon Dec 2 10:18:04 PST 2002


mark.uzumati at virgin.net (Mark Ellis) writes:

> > >          profile_len = strlen(xml)+1;
> > That +1, is that neccessary when you're getting the strlen()?  That
> > looks like an edge condition.
> >
> 
> I wasnt sure whether xmlParseMemory wanted the length of the string
> with or without the terminating NULL, hence the +1, though it doesnt
> work without it either.

I was able to get this bit of code to parse the latest.xml file in
Neven's latest nALFS tarball:

#include <stdio.h>
#include <libxml/tree.h>
#include <libxml/parser.h>

char* read_profile(void);

int main(void)
{
     char *xml = NULL;
     xmlDocPtr xmldoc = NULL;
     int profile_len;

/* reads a file and stores it all in a string */
     xml = read_profile();
     if (xml == NULL) {
          return 0;
     }
     profile_len = strlen(xml);
     if ((xmldoc = xmlParseMemory(xml, profile_len)) == NULL) {
          fprintf(stderr, "Profile parse error : %s\n", xml);
          return 0;
     }
     return 0;
}

char* read_profile(void)
{
     FILE *infile=0;
     char *profile_string=0;
     char str1[256]="", str2[2048]="";

     infile=fopen("latest.xml", "r");
     while(fgets(str1, (int)sizeof(str1), infile))
          strcat(str2, str1);

     profile_string=(char*)malloc(strlen(str2));
     strcpy(profile_string,str2);

     return profile_string;
}
-- 
Unsubscribe: send email to listar at linuxfromscratch.org
and put 'unsubscribe alfs-discuss' in the subject header of the message



More information about the alfs-discuss mailing list