Min Expat bliver ved med at meddele "junk after document element". Jeg har
prøvet forskellige encodings, og jeg er sikker på at bufferen er
nul-termineret. Er der nogle Expat hajer der kan give et tip eller to?
-----Kildekode-----
#include <stdio.h>
#include <expat.h>
void startElement(void *userData, const char *name, const char **atts)
{
printf("Got element: %S\nwith userData:\n%s\n", name, (char *)userData);
}
void endElement(void *userData, const char *name)
{
}
int main(int argc, char *argv[])
{
FILE *fp;
char *buffer;
char *prog = argv[0];
long fsize;
XML_Parser parser;
int userData = 0;
int done;
if(argc == 1) return 0;
if ((fp = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "%s: Can't open %s", prog, *argv);
exit(1);
} else {
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
rewind(fp);
buffer = (char *)malloc(fsize+1);
if (buffer == NULL)
exit(2);
fread(buffer, 1, fsize, fp);
buffer[fsize] = '\0';
printf("%s\n", buffer);
fclose(fp);
parser = XML_ParserCreate((XML_Char *)"ISO-8859-1");
XML_SetUserData(parser, &userData);
XML_SetElementHandler(parser, startElement, endElement);
do {
done = fsize < sizeof(buffer);
if (!XML_Parse(parser, buffer, fsize, 0)) {
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
} while (!done);
XML_ParserFree(parser);
}
return 0;
}
-------------------
-----XML input-----
<?xml version="1.0" ?>
<a>
</a>
-------------------
/Jakob
|