reader7.py 1.7 KB

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