Hi ,
I am new to
XML Parsing. I was supposed to Parse XML Doc , I did that using libxml DOM
parser. But now i need to do it's validation also using DTD , My
queries are:
1). According to me DTD using which validation is to be specified in
XML file itself. Is it possible , DTD file need not be specified in
XML File and still be used for validation purpose.?
Example:
<?xml version="1.0"?>
<!DOCTYPE Sample SYSTEM "d.dtd">
2). I know of API xmlCtxtReadFile to get a ctxt for XML file and then
check the validity of
ctxt->valid to know if File has passed validation or
not.
Consider this :
static void
exampleFunc(const char *filename) {
xmlParserCtxtPtr ctxt; /* the parser context */
xmlDocPtr doc; /* the resulting document tree */
/* create a parser context */
ctxt = xmlNewParserCtxt();
if (ctxt == NULL) {
fprintf(stderr, "Failed to allocate parser
context\n");
return;
}
/* parse the file, activating the DTD validation option */
doc = xmlCtxtReadFile(ctxt, filename, NULL, XML_PARSE_DTDVALID);
/* check if parsing suceeded */
if (doc == NULL) {
fprintf(stderr, "Failed to parse %s\n",
filename);
} else {
/* check if validation suceeded */
if (ctxt->valid == 0)
fprintf(stderr, "Failed to
validate %s\n", filename);
/* free up the resulting document */
xmlFreeDoc(doc);
}
/* free up the parser context */
xmlFreeParserCtxt(ctxt);
}
But this would not tell me which line/element failed
validation? Any other way using libxml DOM.A way where I can do element by
element validation.
I hope there are APIs to do that. Pls help me with some code snippet or
Documentation.
Thx in Advance,
Rachita