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 / build.py < prev    next >
Encoding:
Python Source  |  2002-03-03  |  1.5 KB  |  60 lines

  1. #!/usr/bin/python -u
  2. import libxml2
  3. import sys
  4.  
  5. # Memory debug specific
  6. libxml2.debugMemory(1)
  7.  
  8. doc = libxml2.newDoc("1.0")
  9. comment = doc.newDocComment("This is a generated document")
  10. doc.addChild(comment)
  11. pi = libxml2.newPI("test", "PI content")
  12. doc.addChild(pi)
  13. root = doc.newChild(None, "doc", None)
  14. ns = root.newNs("http://example.com/doc", "my")
  15. root.setNs(ns)
  16. elem = root.newChild(None, "foo", "bar")
  17. elem.setBase("http://example.com/imgs")
  18. elem.setProp("img", "image.gif")
  19. doc.saveFile("tmp.xml")
  20. doc.freeDoc()
  21.  
  22. doc = libxml2.parseFile("tmp.xml")
  23. comment = doc.children
  24. if comment.type != "comment" or \
  25.    comment.content != "This is a generated document":
  26.    print "error rereading comment"
  27.    sys.exit(1)
  28. pi = comment.next
  29. if pi.type != "pi" or pi.name != "test" or pi.content != "PI content":
  30.    print "error rereading PI"
  31.    sys.exit(1)
  32. root = pi.next
  33. if root.name != "doc":
  34.    print "error rereading root"
  35.    sys.exit(1)
  36. ns = root.ns()
  37. if ns.name != "my" or ns.content != "http://example.com/doc":
  38.    print "error rereading namespace"
  39.    sys.exit(1)
  40. elem = root.children
  41. if elem.name != "foo":
  42.    print "error rereading elem"
  43.    sys.exit(1)
  44. if elem.getBase(None) != "http://example.com/imgs":
  45.    print "error rereading base"
  46.    sys.exit(1)
  47. if elem.prop("img") != "image.gif":
  48.    print "error rereading property"
  49.    sys.exit(1)
  50.  
  51. doc.freeDoc()
  52.  
  53. # Memory debug specific
  54. libxml2.cleanupParser()
  55. if libxml2.debugMemory(1) == 0:
  56.     print "OK"
  57. else:
  58.     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  59.     libxml2.dumpMemory()
  60.