Check XML file against DTD file using expat in C



I need to build an application the should parse an XML file using EXPAT and also check it against a DTD file. The problem is how do I do this, I checked the documentation, and couldn't find my way around.


My code so far:



int main(int argc, char *argv[])
{
XML_Parser p = XML_ParserCreate(NULL);
if (!p)
{
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
FILE* xmlFile;
xmlFile = fopen("test.xml", "r");
XML_SetElementHandler(p, start, end);
XML_SetParamEntityParsing(p, XML_PARAM_ENTITY_PARSING_ALWAYS);

for (;;)
{
int done;
int len;

len = (int) fread(Buff, 1, BUFFSIZE, xmlFile);
if (ferror(xmlFile))
{
fprintf(stderr, "Read error\n");
exit(-1);
}
done = feof(xmlFile);

if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR)
{
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}

if (done)
break;
}
XML_ParserFree(p);
return 0;
}


I guess that the should be a call to the XML_SetExternalEntityRefHandler function, but I fail to understand how to use it.


Thank you in advance.


No comments:

Post a Comment