int xml_validation(char *xsd_file, char *xml_file)
{
xmlDocPtr doc;
xmlSchemaPtr schema = NULL;
xmlSchemaParserCtxtPtr ctxt;
char *temp_buf = malloc((strlen(xml_file)+1) * sizeof(char));
char *XMLFileName = malloc((strlen(xml_file)+1) * sizeof(char));
char *XSDFileName = malloc((strlen(xsd_file)+1) * sizeof(char));
strcpy(XMLFileName, xml_file);
strcpy(XSDFileName, xsd_file);
xmlLineNumbersDefault(1);
ctxt = xmlSchemaNewParserCtxt(XSDFileName);
xmlSchemaSetParserErrors(ctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
schema = xmlSchemaParse(ctxt);
xmlSchemaFreeParserCtxt(ctxt);
doc = xmlReadFile(XMLFileName, NULL, 0);
if (doc == NULL){
printf("Error : parsing\n");
}
else{
xmlSchemaValidCtxtPtr ctxt;
int ret = 0;
ctxt = xmlSchemaNewValidCtxt(schema);
xmlSchemaSetValidErrors(ctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
ret = xmlSchemaValidateDoc(ctxt, doc);
if (ret == 0){
strcpy(temp_buf, XMLFileName);
table(temp_buf);
}
xmlSchemaFreeValidCtxt(ctxt);
xmlFreeDoc(doc);
}
xmlSchemaFree(schema);
xmlSchemaCleanupTypes();
xmlCleanupParser();
xmlMemoryDump();
if(temp_buf != NULL)//
free(temp_buf);//program crash if i free temp_buf
if(XMLFileName != NULL)
free(XMLFileName);
if(XSDFileName != NULL)
free(XSDFileName);
return 0;
}
Above code get the xml and xsd file from function call, validate the xml file with xsd file.
If it valid it will send the xml file to another function to create list..
Problem :
If i free temp_buf program gets crash.
If i not freeing program working fine but memory leak will happen because of malloc.
I trying this in linux using gcc compiler.
please someone correct me.