home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-libxml2 / examples / pushSAX.py < prev    next >
Encoding:
Python Source  |  2002-03-03  |  1.5 KB  |  65 lines

  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4.  
  5. # Memory debug specific
  6. libxml2.debugMemory(1)
  7.  
  8. log = ""
  9.  
  10. class callback:
  11.     def startDocument(self):
  12.         global log
  13.         log = log + "startDocument:"
  14.  
  15.     def endDocument(self):
  16.         global log
  17.         log = log + "endDocument:"
  18.  
  19.     def startElement(self, tag, attrs):
  20.         global log
  21.         log = log + "startElement %s %s:" % (tag, attrs)
  22.  
  23.     def endElement(self, tag):
  24.         global log
  25.         log = log + "endElement %s:" % (tag)
  26.  
  27.     def characters(self, data):
  28.         global log
  29.         log = log + "characters: %s:" % (data)
  30.  
  31.     def warning(self, msg):
  32.         global log
  33.         log = log + "warning: %s:" % (msg)
  34.  
  35.     def error(self, msg):
  36.         global log
  37.         log = log + "error: %s:" % (msg)
  38.  
  39.     def fatalError(self, msg):
  40.         global log
  41.         log = log + "fatalError: %s:" % (msg)
  42.  
  43. handler = callback()
  44.  
  45. ctxt = libxml2.createPushParser(handler, "<foo", 4, "test.xml")
  46. chunk = " url='tst'>b"
  47. ctxt.parseChunk(chunk, len(chunk), 0)
  48. chunk = "ar</foo>"
  49. ctxt.parseChunk(chunk, len(chunk), 1)
  50. ctxt=None
  51.  
  52. reference = "startDocument:startElement foo {'url': 'tst'}:characters: bar:endElement foo:endDocument:"
  53. if log != reference:
  54.     print "Error got: %s" % log
  55.     print "Exprected: %s" % reference
  56.     sys.exit(1)
  57.  
  58. # Memory debug specific
  59. libxml2.cleanupParser()
  60. if libxml2.debugMemory(1) == 0:
  61.     print "OK"
  62. else:
  63.     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  64.     libxml2.dumpMemory()
  65.