6 Years
Discussion Span
Last Post by N1GHTS

I use the expat library in my application.


Its robust, easy to use, open source, and recommended by the XML organization. I highly recommend it.

It uses callback functions, so what you do is open your file with fopen, then loop through each character and feed it to XML_Parse() like this...

int Parse(FILE *in_Stream) {

  XML_Parser p = XML_ParserCreate(NULL);
  if (! p) {
    fprintf(stderr, "Couldn't allocate memory for parser\n");
    return -1;

  XML_SetElementHandler(p, start_hndl, end_hndl);
  XML_SetCharacterDataHandler(p, char_hndl);
  XML_SetProcessingInstructionHandler(p, proc_hndl);

  char buff;
  int  done = 0;
  for (fread(&buff, sizeof(char), 1, in_Stream); !feof(in_Stream); fread(&buff, sizeof(char), 1, in_Stream))
     if (! XML_Parse(p, &buff, sizeof(char), done)) {
        fprintf(stderr, "Parse error at line %d:\n%s\n",
        return -1;
  return 0;


Then you use its callback functions to deal with the individual elements. The XML_Parse() function will call those event functions as soon as it detects that a new complete element has been received.

For instance, start_hndl() is called by the XML engine to tell you when something like "<Image" is found. It gives you the string "Image". If it encounters a "/>" or the tag was formatted like this "</Image>", it will call end_hndl() passing the same name "Image", so you know which tag it belongs to.

The sample program that comes with the library should be very easy to understand.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.