[xml-bindings]registerErrorHandler?



I found a message about the RELAX NG support and tried it.  I
thought, "Wow".  So I tried it.  And, it works great.

However, I was not able to make the error callback work.

The file libxml2-2.5.11/python/tests/error.py registers a callback. 
But, it does not seem to have any effect.  Is there something that
I'm missing?

I'm using:

    libxml2-2.5.11
    libxml2-python-2.5.11

I also wrote some test code:

====================================================
import libxml2

libxml2.debugMemory(1)
libxml2.lineNumbersDefault(1)

#
# Error and warnng callbacks
#
def callback(ctx, str):
    print '*' * 40
    print "*** %s%s" % (ctx, str)
    print '*' * 40

libxml2.registerErrorHandler(callback, "")

def validate(schemaFileName, instanceFileName, verbose):
    schemaFile = file(schemaFileName, 'r')
    schema = schemaFile.read()
    schemaFile.close()
    instanceFile = file(instanceFileName, 'r')
    instance = instanceFile.read()
    instanceFile.close()
    rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
    rngs = rngp.relaxNGParse()
    ctxt = rngs.relaxNGNewValidCtxt()
    doc = libxml2.parseDoc(instance)
    ret = doc.relaxNGValidateDoc(ctxt)
    if ret != 0:
        print "error doing RelaxNG validation"
        sys.exit(1)
    doc.freeDoc()
    del rngp
    del rngs
    del ctxt
    libxml2.relaxNGCleanupTypes()
    # Memory debug specific
    libxml2.cleanupParser()
    if libxml2.debugMemory(1) == 0:
        print "OK"
    else:
        print "Memory leak %d bytes" % (libxml2.debugMemory(1))
        libxml2.dumpMemory()

====================================================

But, my callback() function does not get called when there is an
error.

I learned about the RELAX NG support and the error handler when I
did a Web search and found this message:

====================================================
On Thu, Jul 10, 2003 at 05:48:37PM +0200, Martijn Faassen wrote:
> Hey,
> 
> I've been playing with RELAX NG recently; pretty nice. I then
wrote 
> a python script to be able to do the validation from Python,
following
> the relaxng example in the tests directory of the Python
bindings.
> I noticed that upon validation failure an error is reported
directly
> to stdout. In a Python program that isn't really desirable, so I
wonder
> whether it is possible to suppress this or if this indicates a
problem?

  Hi Martijn,

You can do something like:

-----
libxml2.lineNumbersDefault(1)
#
# Error and warnng callbacks
#
def callback(ctx, str):
    global log
    log.write("%s%s" % (ctx, str))
                                                                              
libxml2.registerErrorHandler(callback, "")
-----

to catch all libxml2 messages.

Daniel
====================================================

I thought, "Wow".  So I tried it 

-- 
Dave Kuhlman
dkuhlman rexx com
http://www.rexx.com/~dkuhlman



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