reader5.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/python -u
  2. #
  3. # this tests the Expand() API of the xmlTextReader interface
  4. # this extract the Dragon bibliography entries from the XML specification
  5. #
  6. import libxml2
  7. import StringIO
  8. import sys
  9. # Memory debug specific
  10. libxml2.debugMemory(1)
  11. expect="""<bibl id="Aho" key="Aho/Ullman">Aho, Alfred V.,
  12. Ravi Sethi, and Jeffrey D. Ullman.
  13. <emph>Compilers: Principles, Techniques, and Tools</emph>.
  14. Reading: Addison-Wesley, 1986, rpt. corr. 1988.</bibl>"""
  15. f = open('../../test/valid/REC-xml-19980210.xml')
  16. input = libxml2.inputBuffer(f)
  17. reader = input.newTextReader("REC")
  18. res=""
  19. while reader.Read():
  20. while reader.Name() == 'bibl':
  21. node = reader.Expand() # expand the subtree
  22. if node.xpathEval("@id = 'Aho'"): # use XPath on it
  23. res = res + node.serialize()
  24. if reader.Next() != 1: # skip the subtree
  25. break;
  26. if res != expect:
  27. print "Error: didn't get the expected output"
  28. print "got '%s'" % (res)
  29. print "expected '%s'" % (expect)
  30. #
  31. # cleanup
  32. #
  33. del input
  34. del reader
  35. # Memory debug specific
  36. libxml2.cleanupParser()
  37. if libxml2.debugMemory(1) == 0:
  38. print "OK"
  39. else:
  40. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  41. libxml2.dumpMemory()