[xml] possible limitation in validation callbacks



Hi,

I'm currently trying to override the error callbacks when parsing and 
validating an XML doc which resides in memory. (see example code below)

The overriden callbacks works fine for the Parser (using ctxt->sax callbacks) 
but not for the validation.
The thing is that xmlCtxtReadMemory() calls xmlCtxtReset(), which happens to 
reset the callbacks and the userData in ctxt->vctxt (the validity context).

This really annoys me 'cause I'm using libxml in a multithreaded environement 
and I need to know in which thread the error happened (more specifically in 
which object in C++).

Am I doing something wrong?
Is there an other way to set callbacks/userdata on a per-context basis?

I tried doing the validity check by calling xmlValidateDocument() after 
calling xmlCtxtReadMemory(). But the latter ignore the facts that I don't 
want to validate the document (I comment out the XML_PARSE_DTDVALID flag). 
And the former doesn't give the same error messages (there are less 
complete).

I'm using libxml 2.6.7

Thanks for your help.
Best regards,

Tanguy


void exampleFunc()
{
    xmlParserCtxtPtr ctxt;
    xmlDocPtr doc;

    // create a parser context
    ctxt = xmlNewParserCtxt();

    if (ctxt == NULL) {
        std::cerr << "Failed to allocate parser context" << std::endl;
                return;
    }

        ctxt->_private = reinterpret_cast<void *>(0xF00BA4);
        ctxt->vctxt.userData = reinterpret_cast<void *>(ctxt);

        ctxt->sax->error = ErrorHandler;
        ctxt->sax->warning = ErrorHandler;
        ctxt->vctxt.error = ErrorHandler;
        ctxt->vctxt.warning = ErrorHandler;

    // parse the file, activating the DTD validation option
        doc = xmlCtxtReadMemory(
                ctxt,
                XMLDocument,
                std::strlen(XMLDocument),
                "example.xml",
                NULL,
                XML_PARSE_DTDVALID | XML_PARSE_NONET
        );

        // check if parsing suceeded
    if (doc == NULL) {
        std::cerr << "Failed to parse" << std::endl;
    } else {

                // check if validation suceeded
        if (ctxt->valid == 0)
                std::cerr << "Failed to validate" << std::endl;

                // free up the resulting document
                xmlFreeDoc(doc);
    }

    // free up the parser context
    xmlFreeParserCtxt(ctxt);
}









[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]