[xml] Another structured error question



Hi Daniel, All,

I'm trying to catch parsing errors via the structured error handler
API. I set up the structured handler, NULL the generic handler and
call xmlReadMemory (or similar). But the parsing error is still
printed on stderr via xmlParserError and I can't catch it. If I set
generic handler, it catches the error (but is called several times for
one error message, which is AFAIR also something you said you were
trying to eliminate in 2.6.x).

Is it a bug I can add to:
http://bugzilla.gnome.org/show_bug.cgi?id=126793
?

I only found the following workaround so far:

1) create ctxt, 
2) setup generic handler to a (non-NULL!) function doing nothing
   (to suppress the xmlParserError print on stderr)
3) call xmlParseDocument(ctxt)
4) check the error in ctxt->lastError

Debugging down to __xmlRaiseError, I see the reason for this: channel
is set to ctxt->sax->error (which defaults to xmlParserError) just
before the error could possibly fall on a default xmlStructuredError.
And because serror is still NULL, channel is used.  That doesn't seem
easy to fix for a general case :-(

  Petr

___BEGIN_TEST_CASE___
#include <unistd.h>
#include <libxml/parser.h>
#include <libxml/xmlerror.h>
#include <libxml/tree.h>

char* xmldoc = 
"<?xml version='1.0'?>"
"<foo></bar>";

void
LibXML_serror_handler(void *userData, xmlErrorPtr err) {
    printf("SERROR invoked\n");
}


int
main (void) {
    xmlDocPtr doc;
    xmlSetGenericErrorFunc( NULL ,  NULL);
    xmlSetStructuredErrorFunc( NULL , (xmlStructuredErrorFunc)LibXML_serror_handler);
    doc = xmlReadMemory(xmldoc, strlen(xmldoc), NULL, NULL, 0);
    xmlFree(doc);
}
___END_TEST_CASE___

-- 



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