[Expat-discuss] Always reports utf-8 encoding?

Franky Braem franky.braem at gmail.com
Fri Sep 29 22:24:20 CEST 2006


Karl Waclawek wrote:
> Maybe you can post a small self-contained example program that shows 
> the problem.
>
> Karl
>
The following is a small example:

#include "expat.h"

XML_Char buffer[1000];
int length = 0;


void EndElementHandler(void *userData,
                       const XML_Char *name);

void CharacterDataHandler(void *userData,
                          const XML_Char *s,
                          int len);

int _tmain(int argc, _TCHAR* argv[])
{
    XML_Parser parser = XML_ParserCreate(NULL);
    XML_SetUserData(parser, NULL);
    XML_SetElementHandler(parser, NULL, EndElementHandler);
    XML_SetCharacterDataHandler(parser, CharacterDataHandler);

    FILE *f = fopen("c:\\temp\\modules.xml", "r");
    if ( f )
    {
      // obtain file size.
      fseek (f , 0 , SEEK_END);
      long lSize = ftell (f);
      rewind (f);

      // allocate memory to contain the whole file.
      char *readbuffer = (char*) malloc (lSize);
      if (readbuffer == NULL) exit (2);

      // copy the file into the buffer.
      fread (readbuffer,1,lSize,f);
      XML_Parse(parser, readbuffer, lSize, 1);
      free(readbuffer);
    }
    XML_ParserFree(parser);

    return 0;
}

void EndElementHandler(void *userData,
                       const XML_Char *name)
{
    length = 0;
}

void CharacterDataHandler(void *userData,
                          const XML_Char *s,
                          int len)
{
    for(int i = 0; i < len; i++, length++)
    {
        buffer[length] = s[i];
    }
}

The following is defined:

WIN32;_DEBUG;_CONSOLE;XML_UNICODE;XML_STATIC

And I link with libexpatwMT.lib

When I debug the above, the name of the tags are always readable, while 
I expect some UTF-16 characters.



More information about the Expat-discuss mailing list