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 / validate.py < prev    next >
Encoding:
Python Source  |  2004-02-23  |  1.7 KB  |  83 lines

  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4.  
  5. # Memory debug specific
  6. libxml2.debugMemory(1)
  7.  
  8. ctxt = libxml2.createFileParserCtxt("valid.xml")
  9. ctxt.validate(1)
  10. ctxt.parseDocument()
  11. doc = ctxt.doc()
  12. valid = ctxt.isValid()
  13.  
  14. if doc.name != "valid.xml":
  15.     print "doc.name failed"
  16.     sys.exit(1)
  17. root = doc.children
  18. if root.name != "doc":
  19.     print "root.name failed"
  20.     sys.exit(1)
  21. if valid != 1:
  22.     print "validity chec failed"
  23.     sys.exit(1)
  24. doc.freeDoc()
  25.  
  26. i = 1000
  27. while i > 0:
  28.     ctxt = libxml2.createFileParserCtxt("valid.xml")
  29.     ctxt.validate(1)
  30.     ctxt.parseDocument()
  31.     doc = ctxt.doc()
  32.     valid = ctxt.isValid()
  33.     doc.freeDoc()
  34.     if valid != 1:
  35.         print "validity check failed"
  36.         sys.exit(1)
  37.     i = i - 1
  38.  
  39. #desactivate error messages from the validation
  40. def noerr(ctx, str):
  41.     pass
  42.  
  43. libxml2.registerErrorHandler(noerr, None)
  44.  
  45. ctxt = libxml2.createFileParserCtxt("invalid.xml")
  46. ctxt.validate(1)
  47. ctxt.parseDocument()
  48. doc = ctxt.doc()
  49. valid = ctxt.isValid()
  50. if doc.name != "invalid.xml":
  51.     print "doc.name failed"
  52.     sys.exit(1)
  53. root = doc.children
  54. if root.name != "doc":
  55.     print "root.name failed"
  56.     sys.exit(1)
  57. if valid != 0:
  58.     print "validity chec failed"
  59.     sys.exit(1)
  60. doc.freeDoc()
  61.  
  62. i = 1000
  63. while i > 0:
  64.     ctxt = libxml2.createFileParserCtxt("invalid.xml")
  65.     ctxt.validate(1)
  66.     ctxt.parseDocument()
  67.     doc = ctxt.doc()
  68.     valid = ctxt.isValid()
  69.     doc.freeDoc()
  70.     if valid != 0:
  71.         print "validity check failed"
  72.         sys.exit(1)
  73.     i = i - 1
  74. del ctxt
  75.  
  76. # Memory debug specific
  77. libxml2.cleanupParser()
  78. if libxml2.debugMemory(1) == 0:
  79.     print "OK"
  80. else:
  81.     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  82.     libxml2.dumpMemory()
  83.