bool XMLReader::isValid(const QString &_inFile) { LIBXML_TEST_VERSION //reset the generic error handler initGenericErrorDefaultFunc(NULL); QString ErrorString; //supply custom error handler xmlSetStructuredErrorFunc(&ErrorString, XMLReader::xmlErrorHandler); xmlParserCtxtPtr Context; /* the parser context */ xmlDocPtr Doc; /* the resulting document tree */ /* create a parser context */ Context = xmlNewParserCtxt(); if (Context == NULL) throw exInvalidXML("Failed to allocate parser context"); QFile File(_inFile); File.open(QIODevice::ReadOnly); int FD = File.handle(); /* parse the file, activating the DTD validation option */ Doc = xmlCtxtReadFd(Context, FD, ".", NULL, XML_PARSE_DTDVALID); /* check if parsing suceeded */ if (Doc == NULL) throw exInvalidXML("Unable to read XML Buffer"); else xmlFreeDoc(Doc); /* free up the parser context */ xmlFreeParserCtxt(Context); return true; }
static bool is_dtd_valid(FILE *input, const char *filename) { bool rc = true; #if HAVE_LIBXML xmlParserCtxtPtr ctx = NULL; xmlDocPtr doc = NULL; xmlDtdPtr dtd = NULL; xmlValidCtxtPtr dtdctx; xmlParserInputBufferPtr buffer; int fd = fileno(input); dtdctx = xmlNewValidCtxt(); ctx = xmlNewParserCtxt(); if (!ctx || !dtdctx) abort(); buffer = xmlParserInputBufferCreateMem(&DTD_DATA_begin, DTD_DATA_len, XML_CHAR_ENCODING_UTF8); if (!buffer) { fprintf(stderr, "Failed to init buffer for DTD.\n"); abort(); } dtd = xmlIOParseDTD(NULL, buffer, XML_CHAR_ENCODING_UTF8); if (!dtd) { fprintf(stderr, "Failed to parse DTD.\n"); abort(); } doc = xmlCtxtReadFd(ctx, fd, filename, NULL, 0); if (!doc) { fprintf(stderr, "Failed to read XML\n"); abort(); } rc = xmlValidateDtd(dtdctx, doc, dtd); xmlFreeDoc(doc); xmlFreeParserCtxt(ctx); xmlFreeDtd(dtd); xmlFreeValidCtxt(dtdctx); /* xmlIOParseDTD consumes buffer */ if (lseek(fd, 0, SEEK_SET) != 0) { fprintf(stderr, "Failed to reset fd, output would be garbage.\n"); abort(); } #endif return rc; }
void load(const int fd, xml_node &node) { xmlDocPtr doc = xmlCtxtReadFd(ctx_, fd, url_, encoding_, options_); load(doc, node); }