J
Jakob Møbjerg Nielsen
Expat keeps telling me that there is "junk after document element". I've tried different encoding, and I'm quite sure that the
buffer is nul-terminated. I really have no idea to what the problem might be. Any ideas?
X-POST: comp.lang.c, comp.text.xml (I don't know which group is the right one)
-----Source code-----
#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>
buffer is nul-terminated. I really have no idea to what the problem might be. Any ideas?
X-POST: comp.lang.c, comp.text.xml (I don't know which group is the right one)
-----Source code-----
#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>