如何使用libxml2的使用模式文件在C编程语言验证XML文件 [英] How to validate an xml file using a schema file in C programming language using libxml2
问题描述
我一直在尝试使用架构文件在我的C code验证XML文件。验证发生成功说明该文件是否是有效还是无效。
I have been trying to validate XML file using schema file in my C Code. The validation happens successfully stating whether the file is valid or invalid.
但我的问题是,它打印有效/无效而已。应该有,以什么失踪的XML文件中情况下,它是无效的报告/输出。可能类似于XML文件中的行号。
But my issue is it prints valid/invalid only. There should be a report/output as to what was missing in the xml file in case it was invalid. May be something like the line number in XML file.
希望,我已经作出了明确的自我。
Hope, i have made my self clear.
下面是我的C code: -
Here's my C code:-
int validateXmlFile()
{
int iError = 0;
xmlDocPtr pDoc;
xmlDocPtr pSchemaDoc;
xmlSchemaParserCtxtPtr pSchemaCtxt;
xmlSchemaPtr pSchema;
xmlSchemaValidCtxtPtr pValidCtxt;
char * xmlFilename = "C:\\Documents and Settings\\pbhatia\\Desktop\\Schema\\ipt_config.xml";
char * schemaFilename = "C:\\Documents and Settings\\pbhatia\\Desktop\\Schema\\ipt_config.xsd";
PRNT(printf("Schema file : %s \n", schemaFilename));
PRNT(printf("XML file : %s \n", xmlFilename));
pDoc = xmlReadFile(xmlFilename, NULL, XML_PARSE_NONET);
if (!pDoc)
return -1;
pSchemaDoc = xmlReadFile(schemaFilename, NULL, XML_PARSE_NONET);
if (!pSchemaDoc)
return -2;
pSchemaCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc);
if (!pSchemaCtxt)
return -3;
pSchema = xmlSchemaParse(pSchemaCtxt);
if (!pSchema)
return -4;
pValidCtxt = xmlSchemaNewValidCtxt(pSchema);
if(!pValidCtxt)
return -5;
// Attempting to validate xml with schema
xmlSchemaFreeParserCtxt(pSchemaCtxt);
xmlFreeDoc(pSchemaDoc);
iError = xmlSchemaValidateDoc(pValidCtxt, pDoc);
if (iError == 0)
PRNT(printf("Document in %s is valid \n", xmlFilename));
else
PRNT(printf("Document in %s is NOT valid \n", xmlFilename));
xmlSchemaFree(pSchema);
xmlFreeDoc(pDoc);
return 0;
}
谢谢,
普里
Thanks, Priyanka
推荐答案
从阅读 xmllint.c
源$ C $ C事实证明,你可以在安装回调的背景下,错误和警告,使用的 xmlSchemaSetValidErrors
。在简单的情况下,你转发 fprintf中
,它会简单地打印错误。
From reading xmllint.c
source code it turns out that you may setup callbacks for errors and warnings in the context, using xmlSchemaSetValidErrors
. In simplest case you forward fprintf
and it would simply print errors.
xmlSchemaSetValidErrors(ctxt,
(xmlSchemaValidityErrorFunc) fprintf,
(xmlSchemaValidityWarningFunc) fprintf,
stderr);
UTSL:)
这篇关于如何使用libxml2的使用模式文件在C编程语言验证XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!