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 / reader7.py < prev    next >
Encoding:
Python Source  |  2003-10-20  |  1.7 KB  |  103 lines

  1. #!/usr/bin/python -u
  2. #
  3. # this tests the entities substitutions with the XmlTextReader interface
  4. #
  5. import sys
  6. import StringIO
  7. import libxml2
  8.  
  9. # Memory debug specific
  10. libxml2.debugMemory(1)
  11.  
  12. result = ""
  13. def processNode(reader):
  14.     global result
  15.  
  16.     result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
  17.                reader.Name(), reader.IsEmptyElement())
  18.  
  19. #
  20. # Parse a document testing the readerForxxx API
  21. #
  22. docstr="""<foo>
  23. <label>some text</label>
  24. <item>100</item>
  25. </foo>"""
  26. expect="""0 1 foo 0
  27. 1 14 #text 0
  28. 1 1 label 0
  29. 2 3 #text 0
  30. 1 15 label 0
  31. 1 14 #text 0
  32. 1 1 item 0
  33. 2 3 #text 0
  34. 1 15 item 0
  35. 1 14 #text 0
  36. 0 15 foo 0
  37. """
  38. result = ""
  39.  
  40. reader = libxml2.readerForDoc(docstr, "test1", None, 0)
  41. ret = reader.Read()
  42. while ret == 1:
  43.     processNode(reader)
  44.     ret = reader.Read()
  45.  
  46. if ret != 0:
  47.     print "Error parsing the document test1"
  48.     sys.exit(1)
  49.  
  50. if result != expect:
  51.     print "Unexpected result for test1"
  52.     print result
  53.     sys.exit(1)
  54.  
  55. #
  56. # Reuse the reader for another document testing the ReaderNewxxx API
  57. #
  58. docstr="""<foo>
  59. <label>some text</label>
  60. <item>1000</item>
  61. </foo>"""
  62. expect="""0 1 foo 0
  63. 1 14 #text 0
  64. 1 1 label 0
  65. 2 3 #text 0
  66. 1 15 label 0
  67. 1 14 #text 0
  68. 1 1 item 0
  69. 2 3 #text 0
  70. 1 15 item 0
  71. 1 14 #text 0
  72. 0 15 foo 0
  73. """
  74. result = ""
  75.  
  76. reader.NewDoc(docstr, "test2", None, 0)
  77. ret = reader.Read()
  78. while ret == 1:
  79.     processNode(reader)
  80.     ret = reader.Read()
  81.  
  82. if ret != 0:
  83.     print "Error parsing the document test2"
  84.     sys.exit(1)
  85.  
  86. if result != expect:
  87.     print "Unexpected result for test2"
  88.     print result
  89.     sys.exit(1)
  90.  
  91. #
  92. # cleanup
  93. #
  94. del reader
  95.  
  96. # Memory debug specific
  97. libxml2.cleanupParser()
  98. if libxml2.debugMemory(1) == 0:
  99.     print "OK"
  100. else:
  101.     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  102.     libxml2.dumpMemory()
  103.